3

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?

Thomas Schreiter
  • 770
  • 2
  • 10
  • 25
  • I believe the reason you get that error is because you do not have read permission on the directory you are trying to delete. To give this permission, type `chmod +r directory_name` and you should have permissions for it. – BrockLee Nov 25 '14 at 20:21
  • @PiJoules That does not explain why it works when debugging. – Raydel Miranda Nov 25 '14 at 20:34

2 Answers2

7

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)
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
-1

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)
Luke Livingstone
  • 131
  • 1
  • 1
  • 9
  • This really don't improve the OP's code. And neither explain why the OP is getting that error. Besides your answer is too similar to this: http://stackoverflow.com/questions/11660605/python-overwriting-folder-if-it-already-exists – Raydel Miranda Nov 25 '14 at 20:01
  • I agree with @RaydelMiranda – brodegon Aug 03 '21 at 09:31