我是新来使用Spyder的2.3.0和Python 3.4.1我怎样才能在Python拷贝不同的文件类型,同时保持它们的目录结构
我有子目录的目录结构。
与网络上的其他示例不同,我想选择多个文件类型并复制目录结构。我已经在下面尝试过了,它可以正常工作,但一次只需要一种文件类型,而“copytree
”则会跨越(这将非常慢)。
有没有一种方法或不同的方法来简化它,使其更快?
我在想什么我想要做的是:
使文件类型和位置的综合列表(通过目录结构步行)
例如与
fileExt = [".txt", ".doc", ".docx", ".xls",".xlsx", ".ppt", ".pptx", ".m", ".xmcd", ".pdf " ]
结尾然后该列表只是“shutil.copytree
”
任何建议非常感谢。
srcDir = 'c:/a/src/dir/'
dirName = 'c:/a/dest/dir/'
import os
import shutil
##################################################################################
dstDir = os.path.abspath(dirName)
def ignore_list(path, files):
filesToIgnore = []
for fileName in files:
fullFileName = os.path.join(os.path.normpath(path), fileName)
if not os.path.isdir(fullFileName) and not fileName.endswith('.txt') :
filesToIgnore.append(fileName)
return filesToIgnore
# start of script
shutil.copytree(srcDir, dstDir, ignore=ignore_list)
####################################################################################################################################################################
dstDir = os.path.abspath(dirName)
def ignore_list(path, files):
filesToIgnore = []
for fileName in files:
fullFileName = os.path.join(os.path.normpath(path), fileName)
if not os.path.isdir(fullFileName) and not fileName.endswith('.docx') :
filesToIgnore.append(fileName)
return filesToIgnore
# start of script
shutil.copytree(srcDir, dstDir, ignore=ignore_list)
####################################################
复制并粘贴改变“的endsWith( 'DOCX。'):”
也许fnmatch的帮助。请看这里:https://docs.python.org/2/library/fnmatch.html – Humbalan