Os.rename on windows raises WindowsError instead of OSError

Python documentation says which

os.rename (src, dst)

... On Windows, if dst already exists, an OSError will be raised, even if it is a file ...

However, for me, this raises a WindowsError. Is there a mistake in the documentation?

The second part of the question (more general, but inspired by the problem formulated above):

UPD Sorry, the second part of the question was wrong. A WindowsError is really caught except OSError , as it should.

+4
source share
1 answer

Since OSError is a superclass of WindowsError, just catch OSError.

FWIW, core developers can create an exception that is more specific than the minimum promised by the documents.

Also, the following code works fine for me (Python2.7.2 works on WindowsXP):

 try: raise os.rename('nonexisting_file', 'def') except OSError: print 'caught' 
+7
source

Source: https://habr.com/ru/post/1391376/


All Articles