【发布时间】:2016-06-29 01:32:13
【问题描述】:
我是使用shutil copytree复制文件夹和主文件夹内的所有子文件夹
import shutil
import sys
import os
import re
SOURCE_FOLDER = sys.argv[1]
DESTINATION_FOLDER = sys.argv[2]
def copyDirectory(SOURCE_FOLDER, DESTINATION_FOLDER):
try:
print SOURCE_FOLDER
print DESTINATION_FOLDER
shutil.copytree(SOURCE_FOLDER, DESTINATION_FOLDER)
# Directories are the same
#except:
# print "Not copied"
except shutil.Error as e:
print('Directory not copied. Error: %s' % e)
# Any error saying that the directory doesn't exist
except OSError as e:
print('Directory not copied. Error: %s' % e)
copyDirectory(SOURCE_FOLDER,DESTINATION_FOLDER)
问题是如果目录存在就会抛出错误
Directory not copied. Error: [Errno 17] File exists: 'destination'
我想要的是如果目录已经存在,它要检查所有子目录,如果子目录也存在,它应该检查其中的所有文件,它应该跳过现有文件并复制该子目录中的新文件,如果子目录不存在,那么它应该复制那个子目录
注意:子目录可能是嵌套的(子目录的子目录)。
但上面的脚本不起作用,我应该在该脚本中添加什么?
【问题讨论】:
-
来自文档“目标目录必须不存在。” 也许您可以复制代码并根据需要进行调整。
标签: python directory copy shutil