How can I make a verify error for this command?
if blablablabla:
os.makedirs('C:\\test\\')
If the folder already exists, he return me an error... how can I make it ignore this error? and move on ?
try:
os.makedirs('C:\\test\\')
except OSError:
pass
You also might want to check the specific "already exists" error (since OSError
could mean other things, like permission denied...
import errno
try:
os.makedirs('C:\\test\\')
except OSError as e:
if e.errno != errno.EEXIST:
raise # raises the error again
In Python3.2 and above, just add exist_ok=True
will solve this problem.
If exist_ok is False (the default), an FileExistsError is raised if the target directory already exists.
os.makedirs('C:\\test\\',exist_ok=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With