T
Tim
I have the following function (Python2.7 on FreeBSD) that results in an OSError.
My intent is to pass it a directory name or path and if it exists, use shutil.rmtree to remove whatever is there (if it isn't a directory, try to unlink it); then use os.makedirs to create a new directory or path:
def make_clean_dir(directory):
if os.path.exists(directory):
if os.path.isdir(directory):
shutil.rmtree(directory)
else:
os.unlink(directory)
os.makedirs(directory)
The last bit of the traceback is:
File "/develop/myproject/helpers/__init__.py", line 35, in make_clean_dir
os.makedirs(directory)
File "/usr/local/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists: '/users/tim/testing/testing_html'
The directory 'testing_html' existed when I executed the function;
So I suppose the directory wasn't finished being removed by the time os.makedirs was invoked. How can avoid this? (A context manager maybe?).
thanks,
--Tim
My intent is to pass it a directory name or path and if it exists, use shutil.rmtree to remove whatever is there (if it isn't a directory, try to unlink it); then use os.makedirs to create a new directory or path:
def make_clean_dir(directory):
if os.path.exists(directory):
if os.path.isdir(directory):
shutil.rmtree(directory)
else:
os.unlink(directory)
os.makedirs(directory)
The last bit of the traceback is:
File "/develop/myproject/helpers/__init__.py", line 35, in make_clean_dir
os.makedirs(directory)
File "/usr/local/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists: '/users/tim/testing/testing_html'
The directory 'testing_html' existed when I executed the function;
So I suppose the directory wasn't finished being removed by the time os.makedirs was invoked. How can avoid this? (A context manager maybe?).
thanks,
--Tim