2016-11-23 108 views
0

我是新来使用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。'):”

+0

也许fnmatch的帮助。请看这里:https://docs.python.org/2/library/fnmatch.html – Humbalan

回答

0

我们可以执行你的 “ignore_list” 方法多一点点

valid_formats = ["txt", "doc", "docx", "xls","xlsx", "ppt", "pptx", "m", "xmcd", "pdf "] 

import timeit 
import os.path as op 

def ignore_list(path, files): 
    dag_path = [op.join(op.normpath(path), f) for f in files] 
    return [ff for ff in dag_path if not op.isdir(ff) and ff.split(".")[-1] not in valid_formats] 

start = timeit.default_timer() 
ignore = ignore_list(path, files) 
print ("Time: {0}".format(str(timeit.default_timer()-start))) 
+0

我很新编程(35岁的机械工程师),就像我在我的工作示例中所示: srcDir ='c :/ a/src/dir /' dirName ='c:/ a/dest/dir /' 请你给我一个完整的例子,我可以使用。 (op.join)(op.normpath(path());我喜欢“valid_formats”的想法 谢谢 – Lost1e

+0

“ignore = ignore_list(path,files)”不断抛出一个错误Spyder – Lost1e

+0

replace in ignore_list ... dag_path = [(op.join(op.normpath ),f))for f in files],()缺少,sry –

0

这工作,我知道它不漂亮,但它的作品

srcDir = 'c:/a/src/dir/ 
dstDir = 'c:/a/dest/dir/' 


A01= '.doc' 
A02= '.docx' 
A03= '.xls' 
A04= '.xlsx' 
A05= '.ppt' 
A06= '.pptx' 
A07= '.m' 
A08= '.xmcd' 
A09= '.inp' 
A10= '.pdf' 
A11= '.DOC' 
A12= '.DOCX' 
A13= '.XLS' 
A14= '.XLSX' 
A15= '.PPT' 
A16= '.PPTX' 
A17= '.M' 
A18= '.XMCD' 
A19= '.INP' 
A20= '.PDF' 



import os 
import shutil 

######################################################################################################### 


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(A01) and not fileName.endswith(A02) and not fileName.endswith(A03) and not fileName.endswith(A04) and not fileName.endswith(A05) and not fileName.endswith(A06) and not fileName.endswith(A07) and not fileName.endswith(A08) and not fileName.endswith(A09) and not fileName.endswith(A10) and not fileName.endswith(A11) and not fileName.endswith(A12) and not fileName.endswith(A13) and not fileName.endswith(A14) and not fileName.endswith(A15) and not fileName.endswith(A16) and not fileName.endswith(A17) and not fileName.endswith(A18) and not fileName.endswith(A19) and not fileName.endswith(A20) : 


      filesToIgnore.append(fileName) 

    return filesToIgnore 




shutil.copytree(srcDir, dstDir, ignore=ignore_list) 

######################################################################################################### 
相关问题