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
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 = {}
distutils.dir_util.copy_tree(src, dst)
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With