Force Overwrite in Os.Rename

后端 未结 7 1630
北荒
北荒 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 05:50

    From the Standard Library documentation, “On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.”

    http://docs.python.org/library/os.html#os.rename

    So the only solution, unfortunately, would be to change operating systems; Windows simply disallows a rename() atop an existing file.

    0 讨论(0)
  • 2020-11-30 05:59

    You could try shutil.move():

    from shutil import move
    
    move('C:\\Users\\Test.txt', 'C:\\Users\\Tests.csv')
    

    Or os.remove and then shutil.move:

    from os import remove
    from shutil import move
    
    remove('C:\\Users\\Tests.csv')
    move('C:\\Users\\Test.txt', 'C:\\Users\\Tests.csv')
    
    0 讨论(0)
  • 2020-11-30 06:04

    Since Python 3.3, there is now a standard cross-platform solution, os.replace:

    Rename the file or directory src to dst. If dst is a directory, OSError will be raised. If dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement).

    Availability: Unix, Windows.

    New in version 3.3.

    However, contrary to the documentation, on Windows it's not guaranteed to be atomic (in Python 3.4.4). That's because internally it uses MoveFileEx on Windows, which doesn't make such a guarantee.

    0 讨论(0)
  • 2020-11-30 06:06

    As the documentation says it's impossible to guarantee an atomic renaming operation on Windows if the file exists so what Python does is asking to do the double step os.remove + os.rename yourself, handling potential errors.

    On unix systems rename overwrites the destination if exists (because the operation is guaranteed to be atomic).

    Note that on windows it's also possible that deleting the destination file will fail even if you have permission because the file may be in use. This is another essential limitation of the windows file system and you have to handle it yourself in the code.

    0 讨论(0)
  • 2020-11-30 06:07

    How about use of replace() and shutil.move() ? example:

    import os
    from shutil import move
    
    filename=r'C:\Users\Test.txt'
    move(filename,filename.replace('.txt','.csv')
    

    also you can make it more general for all the files. Thanks

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题