Guyz - 我有这个问题,shutil.copy 树认为一个目录存在,即使它不存在..源和目录都是本地的......我第一次运行它运行时没有错误,但内容实际上没有被复制,第二次它运行它认为该目录已经存在,详细信息如下..请提供您的输入,如果除了shutil之外还有其他复制方式..请建议
第一次运行,没有任何错误,但实际上并未复制
<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
第二次重新运行,它认为它认为目录存在
<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代码
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()