Reputation: 459
Guyz - I am having this problem where shutil.copy tree thinks a directory exists even though it doesnt..both the source and directory are local...first time i ran it ran without errors but the content is actually not copied,second time it ran it thinks the directory already exists,details below..please provide your inputs,if there are any other ways to copy other than shutil..please suggest
Firs time ran,without any errors but it was not actually copied
<username:/local/mnt/workspace/username/Scripts>python test.py
//local/mnt/workspace/loc/04.01.01.00.303_HY11/out
//local/mnt/workspace/test/out
copying
Rerun second time,it thinks it thinks directory exists
<username:/local/mnt/workspace/username/Scripts>python test.py
//local/mnt/workspace/loc/04.01.01.00.303_HY11/out
//local/mnt/workspace/test/out
copying
Traceback (most recent call last):
File "test.py", line 21, in <module>
main()
File "test.py", line 18, in main
copytree(src,dst)
File "test.py", line 11, in copytree
shutil.copytree(s, d)
File "/pkg/qct/software/python/2.5.2/.amd64_linux26/lib/python2.5/shutil.py", line 110, in copytree
os.makedirs(dst)
File "/pkg/qct/software/python/2.5.2/.amd64_linux26/lib/python2.5/os.py", line 171, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists: '//local/mnt/workspace/test/out'
<username:/local/mnt/workspace/username/Scripts>
Python code
import os,shutil
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
print s
d = os.path.join(dst, item)
print d
if os.path.isdir(s):
print "copying"
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
def main ():
src="//local/mnt/workspace/loc/04.01.01.00.303_HY11"
dst="//local/mnt/workspace/test"
copytree(src,dst)
if __name__ == '__main__':
main()
Upvotes: 0
Views: 1311
Reputation: 35950
Try this version, the destination directory will be automatically cleared...
import os,shutil,errno
def copytree(src, dst, symlinks=False, ignore=None):
if os.path.exists(dst):
shutil.rmtree(dst)
os.mkdir(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
print s + " >> " + d
if ".git" in s:
return
if os.path.isdir(s):
print "Copying directory..."
try:
copytree(s, d, symlinks, ignore)
except OSError as e:
# File already exist
if e.errno == errno.EEXIST:
print "Path exists : " + d
else:
shutil.copy2(s, d)
def main ():
src="//local/mnt/workspace/loc/04.01.01.00.303_HY11"
dst="//local/mnt/workspace/test"
copytree(src,dst)
if __name__ == '__main__':
main()
Upvotes: 1
Reputation: 2978
May I ask why don't you use shutil.copytree()
instead? If you do want to have a wrapper around shutil.copytree()
(for considering existing directory, for example) name your function different such as copytree_wrapper()
(Because you are mixing your copytree
with shutil.copytree
, and recursion is not involving your copytree
) The following works for me:
import os,shutil
def copytree_wrapper(src, dst, symlinks=False, ignore=None): ### Name it different, no confusion!
for item in os.listdir(src):
s = os.path.join(src, item)
print (s)
d = os.path.join(dst, item)
print (d)
if os.path.isdir(s):
print ("copying")
if not os.path.exists(d): ### Create directory if does not already exist
print ("directory '%s' created" % d)
os.makedirs(d)
copytree_wrapper(s, d, symlinks, ignore) ### shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
def main ():
src="/tmp/a"
dst="/tmp/b"
copytree_wrapper(src,dst)
if __name__ == '__main__':
main()
Upvotes: 0