Force Overwrite in Os.Rename

后端 未结 7 1641
北荒
北荒 2020-11-30 05:10

Is it possible to force a rename os.rename to overwrite another file if it already exists? For example in the code below if the file Tests.csv already exists it would be re

7条回答
  •  野性不改
    2020-11-30 06:08

    Funny enough, the documentation for os.rename() says it does replace the target on Unix systems, but on Windows it does not. They mention something vague about it being impossible to implement atomic renaming if the destination exists on Windows, which IMO is hardly enough reason not to support it.

    You should catch OSError (destination exists on Windows) and remove the destination and try again, I suppose.

提交回复
热议问题