2016-11-23 2 views
0

Je suis nouveau à l'aide de Spyder 2.3.0 et Python 3.4.1Comment puis-je copier différents types de fichiers en Python tout en conservant leur structure de répertoires

J'ai une structure de répertoires avec des sous-répertoires.

Contrairement à d'autres exemples sur le Web, je souhaite sélectionner plusieurs types de fichiers et copier la structure du répertoire. J'ai essayé ci-dessous et cela fonctionne mais il ne prend qu'un seul type de fichier à la fois et "copytree" (il va être très lent).

Existe-t-il un moyen ou une façon différente de rationaliser ce processus pour le rendre plus rapide?

Ce que je pensais que je voulais faire était:

Faire une liste complète des types de fichiers et les emplacements (marche à travers la structure de répertoire)

Par exemple se terminant par

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

Puis, avec cette liste simplement "shutil.copytree"

Tout conseil grandement apprécié.

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) 

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

Copiez et collez le changement « endswith ('docx.): »

+0

peut-être aide fnmatch. S'il vous plaît regardez ici: https://docs.python.org/2/library/fnmatch.html – Humbalan

Répondre

0

nous pouvons effectuer votre méthode "ignore_list" un peu plus

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

Je suis très nouveau à la programmation (ingénieur mécanique de 35 ans), comme dans mon exemple de travail que j'ai: srcDir = 'c :/a/src/dir/' dirName =' c:/a/dest/dir/' pourriez-vous s'il vous plaît donnez-moi un exemple complet que je pourrais utiliser. Le J'aime l'idée "valid_formats" Merci – Lost1e

+0

le "ignore = ignore_list (chemin, fichiers)" continue à générer une erreur dans Spyder – Lost1e

+0

remplacer dans ignore_list ... dag_path = [(op.join (op.normpath (chemin), f)) pour f dans les fichiers],() est manquant, sry –

0

Cela fonctionne, je sais son pas joli mais cela fonctionne

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) 

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