tas
tas

Reputation: 413

dir_util.copy_tree fails after shutil.rmtree

I'm trying to copy a folder to another one after it has been deleted:

for i in range(0,3):
   try:
      dir_util.remove_tree("D:/test2")
 #     shutil.rmtree("D:/test2")
      print "removed"
   except: pass

   dir_util.copy_tree("D:/test1", "D:/test2")

   print i

D:/test1 contains one empty file called test_file. If I use dir_util.remove_tree it works fine, but after shutil.rmtree it works only once, on second iteration it fails. Output:

removed
0
removed
Traceback (most recent call last):
  File "test.py", line 53, in <module>
    dir_util.copy_tree("D:/test1", "D:/test2")
  File "C:\Python27\lib\distutils\dir_util.py", line 163, in copy_tree
    dry_run=dry_run)
  File "C:\Python27\lib\distutils\file_util.py", line 148, in copy_file
    _copy_file_contents(src, dst)
  File "C:\Python27\lib\distutils\file_util.py", line 44, in _copy_file_contents
    fdst = open(dst, 'wb')
IOError: [Errno 2] No such file or directory: 'D:/test2\\test_file'

It is more convenient for me to use shutil.rmtree because it allows error handling for removing read-only files. What is the difference between dir_util.remove_tree and shutil.rmtree? Why doesn't copy_tree work after rmtree second time?

I'm running Python 2.7.2 on Windows 7

Upvotes: 11

Views: 5312

Answers (5)

DikobrAz
DikobrAz

Reputation: 3697

Seems to be a bug in distutils. If you copy folder, then remove it, then copy again it will fail, because it caches all the created dirs. To workaround you can clear _path_created before copy:

distutils.dir_util._path_created.clear()
distutils.dir_util.copy_tree(src, dst)

Upvotes: 33

crizCraig
crizCraig

Reputation: 8897

shutil.copytree works!

if os.path.exists(dest):
    shutil.rmtree(dest)
shutil.copytree(src, dest)

Upvotes: 0

Maxus
Maxus

Reputation: 11

There seems to be a lack of consistency in the paths separator. In Windows you should use "\\" (it needs to be escaped). *Nix systems use /.

You can use: os.path.join("D:\\test2", "test_file") to make it OS independent. More info

Upvotes: 1

outcoldman
outcoldman

Reputation: 11832

Please read the documentation about distutil, this module is for "Building and installing Python modules" (https://docs.python.org/2/library/distutils.html)

If you want to copy a directory tree from one place to another you should take a look on shutil.copytree https://docs.python.org/2/library/shutil.html#shutil.copytree

Upvotes: 4

IanNorton
IanNorton

Reputation: 7282

It looks very much like you are getting bitten by the variations of path separators. The main clue is:

IOError: [Errno 2] No such file or directory: 'D:/test2\\test_file'

Which concatenates the filename with the directory name using os.sep. I think you should use the proper path separators if you can.

Upvotes: 0

Related Questions