【问题标题】:Copy Files From List Python从 Python 列表中复制文件
【发布时间】:2013-04-23 15:43:00
【问题描述】:

我正在制作一个小型 python 程序来复制一些文件。我的文件名在“selectedList”列表中。

用户选择了源目录“self.DirFilename”和目标目录“self.DirDest”。

我使用 cp 而不是 shutil,因为我听说过 shutil 很慢。

这是我的代码:

for i in selectedList:
    src_dir = self.DirFilename + "/" + str(i) + ".mov"
    dst_dir = self.DirDest
    r = os.system('cp -fr %s %s' % (src_dir, dst_dir))
    if r != 0:
        print 'An error occurred!'**

我希望副本在源目录中搜索给定的文件名,然后在目标中重新创建文件夹结构并复制文件。

任何建议都会有所帮助(就像我正在犯的任何明显的错误一样)- 这是我的第一个 python 程序,我快到了!

谢谢 加文

【问题讨论】:

  • 为避免有趣的非转义字符(如空格)出现问题和安全问题,请执行以下操作:r = subprocess.call(('cp', '-fr', src_dir, dst_dir + '/'))
  • 仅供参考 shutil 复制速度很慢,因为它的缓冲区大小只有 16K。根据多个来源(例如blogs.blumetech.com/blumetechs-tech-blog/2011/05/…),更大的缓冲区大小可以产生很大的不同。对于递归副本,更改shutil 缓冲区大小并不容易。请参阅上面的链接以获取替代实现可能巨大的差异与不同的磁盘查找模式有关。
  • 这里的问题对我来说并不明显。
  • 谢谢 pts,我会调查这两个。
  • 我想在目录中搜索列表中指定的文件。找到的文件可能是目录深处的几个文件夹。找到文件后,我想将文件及其文件夹结构复制到目标目录。我当前的代码只能在一个目录中找到并复制到另一个目录。这有意义吗?

标签: python wxpython cp shutil


【解决方案1】:

我认为这样的事情可以解决问题。当然你可能想使用一些东西来调用 os.system 来调用 cp。

import os

for r, d, f in os.walk(self.DirFilename):
    for file in f:
        f_name, f_ext = os.path.splitext(file)
        if ".mov" == f_ext:
            if f_name in selectedList:
                src_abs_path = os.path.join(r, file)
                src_relative_path = os.path.relpath(src_abs_path, self.DirFilename)
                dst_abs_path = os.path.join(self.DirDest, src_relative_path)
                dst_dir = os.path.dirname(dst_abs_path)
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                ret = os.system('cp -fr %s %s' % (src_abs_path, dst_abs_path))
                if ret != 0:
                    print 'An error occurred!'

【讨论】:

  • 感谢 Zuljin,这有效,但仅适用于列表中的第一项。你的代码对我来说有点高级,我需要一些时间来弄清楚。感谢您的帮助。
  • 在理解了您的代码之后,我发现它存在一个问题。它对我来说非常有效,但在复制 1 个文件后卡住了。我收到此错误:
  • Traceback(最近一次调用最后):文件“/Users/gavinhinfey/Desktop/ALEXAFROMEDL.py”,第 119 行,在 DoTheCopy src_abs_path = os.path.join(r, file) File"/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py",第 77 行,加入 elif path == '' 或 path.endswith('/'): AttributeError: 'int' object没有属性'endswith'
  • 是的,因为您正在用 os.system 的返回值覆盖变量 r。在下面使用不同的变量名。
【解决方案2】:

有关递归副本的纯 Python 实现,请参阅 http://blogs.blumetech.com/blumetechs-tech-blog/2011/05/faster-python-file-copy.html

您可以使用os.walk找到您需要的文件:

def find_files(...):
    for ... in os.walk(...):
        if ...:
            yield filename

for name in find_files(...):
   copy(name, ...)

【讨论】:

    【解决方案3】:
    import glob
    for fname in selectedList:
        filename = str(fname) + '.mov'
        found = glob.glob(os.path.join(self.DirFilename, filename))
        found.extend(glob.glob(os.path.join(self.DirFilename, '**', filename)))
        found = [(p, os.path.join(self.DirDest, os.path.relpath(p, self.DirFilename))) for p in found]
        for found_file in found:
            # copy files however
            #r = os.system('cp -fr %s %s' % found_file)
    

    【讨论】:

    • found = [(p, os.path.join(self.DirDest, os.path.relpath(p, self.DirFilename))) for p in found] ^ SyntaxError: invalid syntax跨度>
    • @GavinHinfey 奇怪,我没有收到语法错误。它在抱怨什么?
    • 我引用的行中变量“found”的结尾。
    • @GavinHinfey 啊,找到了,之前那行缺少paren,已修复
    猜你喜欢
    • 1970-01-01
    • 2012-05-13
    • 2017-04-22
    • 2020-09-03
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多