2016-11-23 107 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) 

######################################################################################################### 
相關問題