9
In python, I have made a function to make a directory if does not already exist.
def make_directory_if_not_exists(path):
try:
os.makedirs(path)
break
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
On Windows, sometimes I will get the following exception:
WindowsError: [Error 5] Access is denied: 'C:\\...\\my_path'
It seems to happen when the directory is open in the Windows File Browser, but I can't reliably reproduce it. So instead I just made the following workaround.
def make_directory_if_not_exists(path):
while not os.path.isdir(path):
try:
os.makedirs(path)
break
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
except WindowsError:
print "got WindowsError"
pass
What's going on here, i.e. when does Windows mkdir give such an access error? Is there a better solution?