os.rename on Windows

Steven D'Aprano steve at pearwood.info
Wed Mar 23 08:17:35 EDT 2016


Any Windows users here?


According to the documentation, os.rename(original, new) will fail if new
already exists.

Would somebody be kind enough to tell me what OSError is raised? In
particular:

# Untested.
import os
open('a123.junk', 'w')
open('b123.junk', 'w')
try:
    os.rename('a123.junk', 'b123.junk')
except OSError as e:
    print(e.winerror)  # Windows only
    print(e.errno)
    print(repr(e))

os.unlink('a123.junk')
os.unlink('b123.junk')



I'd test it myself, except I don't have access to Windows with Python.

Thanks in advance.



-- 
Steven




More information about the Python-list mailing list