我是新來使用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