timing issue: shutil.rmtree and os.makedirs
jtim.arnold at gmail.com
Tue Jul 30 15:10:05 CEST 2013
On Monday, July 29, 2013 7:52:36 PM UTC-4, Chris Angelico wrote:
> On Mon, Jul 29, 2013 at 8:16 PM, Tim wrote:
> > 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;
> First thing I'd check is: Did rmtree succeed? Try removing the
> makedirs and test it again; then, when your process has completely
> finished, see if the directory is there. If it is, the problem is in
> rmtree - for instance:
> * You might not have permission to remove everything
> * There might be a messed-up object in the file system
> * If the directory is a remote share mount point, the other end might
> have lied about the removal
> * Something might have been created inside the directory during the removal
> * Myriad other possibilities
> As I understand rmtree's docs, any errors *that it detects* will be
> raised as exceptions (since you haven't told it to suppress or handle
> them), but possibly there's an error that it isn't able to detect.
> Worth a test, anyhow.
Thanks Chris, but the directory was actually removed on the first run in spite of the traceback; when I run it a second time (immediately after the first time), it runs fine. That's why I thought it was a timing issue. I thought about just putting a sleep in there, but that made me feel dirty.
hmm, now that you mention it, this is executing on a remote box with access to the same file system my local calling program is on. That is, there is a local call to an intermediate script that connects to a socket on the remote where the above program actually runs, but the file system is the same place for both local and remote.
But even so, since the script that does the rmtree and mkdir is running on the same machine (even though it's remote), I would think the mkdir couldn't execute until the rmtree was completely finished.
More information about the Python-list