os.rename on Windows
eryk sun
eryksun at gmail.com
Wed Mar 23 10:16:23 EDT 2016
More information about the Python-list mailing list
Wed Mar 23 10:16:23 EDT 2016
- Previous message (by thread): os.rename on Windows
- Next message (by thread): Not downloading
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Wed, Mar 23, 2016 at 7:17 AM, Steven D'Aprano <steve at pearwood.info> wrote: > > According to the documentation, os.rename(original, new) will fail if new > already exists. In 3.3+ you can use os.replace. For POSIX systems it's functionally the same as os.rename. pyosreplace [1] backports os.replace for 2.6, 2.7 and 3.2. Ignore the comment on the PyPI page that MoveFileEx isn't guaranteed to be atomic. It won't do a CopyFile because os.replace doesn't use the flag MOVEFILE_COPY_ALLOWED. For a cross-volume move, which can't be executed atomically in a single NtSetInformationFile system call, os.replace fails with the Windows error code ERROR_NOT_SAME_DEVICE (0x0011 or 17). [1]: https://pypi.python.org/pypi/pyosreplace > Would somebody be kind enough to tell me what OSError is raised? If the file already exists, os.rename fails with winerror set to ERROR_ALREADY_EXISTS (0x00B7 or 183). If you need to support 2.x, you should handle WindowsError, which is a subclass of OSError that has the "winerror" attribute. In 3.3+ OSError itself has the "winerror" attribute, and in this case WindowsError is just an alias for OSError. For 3.3+ you can also handle the cross-platform FileExistsError subclass of OSError.
- Previous message (by thread): os.rename on Windows
- Next message (by thread): Not downloading
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list