Reputation: 810
I want to create a new directory and remove the old one if it exists. I use the following code:
if os.path.isdir(dir_name):
shutil.rmtree(dir_name)
os.makedirs(dir_name)
It works, if the directory does not exist.
It errors if the directory does exist and the program in run normally. (WindowsError: [Error 5] Access is denied: 'my_directory')
However, it also works if the directory already exists and the program is executed in debug mode line by line. I guess shutil.rmtree()
and makedirs()
need some time in between their calls.
What is the correct code so that it doesn't create an error?
Upvotes: 3
Views: 9259
Reputation: 14360
In Python a statement is executed just when the previous statement have finished, thats how an interpreter works.
My guess is that shutil.rmtree
tell the filesystem to delete some directory tree and in that moment Python gives terminate the work of that statement --even if the filesystem have not deleted the complete directory tree--. For that reason, if the directory tree is big enough, when Python get to the line os.makedirs(dir_name)
the directory can still to exist.
A faster operation (faster than deleting) is to rename the directory:
import os
import tempfile
import shutil
dir_name = "test"
if (os.path.exists(dir_name)):
# `tempfile.mktemp` Returns an absolute pathname of a file that
# did not exist at the time the call is made. We pass
# dir=os.path.dirname(dir_name) here to ensure we will move
# to the same filesystem. Otherwise, shutil.copy2 will be used
# internally and the problem remains.
tmp = tempfile.mktemp(dir=os.path.dirname(dir_name))
# Rename the dir.
shutil.move(dir_name, tmp)
# And delete it.
shutil.rmtree(tmp)
# At this point, even if tmp is still being deleted,
# there is no name collision.
os.makedirs(dir_name)
Upvotes: 7
Reputation: 129
What about this?
import shutil
import os
dir = '/path/to/directory'
if not os.path.exists(dir):
os.makedirs(dir)
else:
shutil.rmtree(dir)
os.makedirs(dir)
Upvotes: -1