If a file already exists how do I get rid of the error 17 and make a warning message instead?
import os, sys
# Path to be created
path = "/tmp/home/monthly/daily"
try:
os.makedirs(path)
except OSError as e:
if e.errno == 17:
//do something
os.makedirs( path, 0755 );
print "Path is created"
However it still shows the ERRNO 17 message. What can I do?
After the first call to the
os.makedirs
the directory will be created. (or no change if the directory was already there)The second call will always raise the exception.
Remove the second call to the
makedirs
:You can create a variable to see if you created the file successfully like this:
In your code if first try catch the error, the second os.makedirs( path, 0755 ); will still raise the error again.
I think does what you want. When an
OSError
error occurs, it checks the error code and prints a warning message if it the one you're interested in handling, otherwise it just propagates the exception. Note the use the optional mode argument toos.makedirs()
which overrides the default of0777
.