【问题标题】:How can I copy different file types in Python while keeping their directory structure如何在 Python 中复制不同的文件类型,同时保持它们的目录结构
【发布时间】:2017-04-07 04:15:57
【问题描述】:

我刚开始使用 Spyder 2.3.0 和 Python 3.4.1

我有一个带有子目录的目录结构。

与网络上的其他示例不同,我想选择多种文件类型并复制目录结构。我在下面尝试过,它可以工作,但一次只需要一种文件类型和“copytree's”(它会很慢)。

有没有办法或不同的方式来简化它以使其更快?

我想我想做的是:

制作文件类型和位置的综合列表(遍历目录结构)

例如以

结尾
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') :”

【问题讨论】:

标签: python operating-system shutil copytree


【解决方案1】:

我们可以多执行一些“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)))

【讨论】:

  • 我对编程非常陌生(35 岁的机械工程师),就像在我的工作示例中一样:srcDir = 'c:/a/src/dir/' dirName = 'c:/ a/dest/dir/' 你能给我一个我可以使用的完整例子吗?我喜欢“valid_formats”的想法谢谢
  • "ignore = ignore_list(path, files)" 在 Spyder 中不断抛出错误
  • replace in ignore_list...dag_path = [(op.join(op.normpath(path), f)) for f in files], () is missing, sry
【解决方案2】:

这行得通,我知道它不漂亮,但它行得通

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)

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

【讨论】:

    猜你喜欢
    • 2012-08-17
    • 2016-07-08
    • 1970-01-01
    • 2018-10-20
    • 2018-11-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2013-03-16
    相关资源
    最近更新 更多