【问题标题】:Is there a way to move many files quickly in Python?有没有办法在 Python 中快速移动许多文件?
【发布时间】:2010-10-09 07:31:06
【问题描述】:

我有一个小脚本可以在我的照片集中移动文件,但运行速度有点慢。

我认为这是因为我一次只移动一个文件。我猜如果我同时将所有文件从一个目录移动到另一个目录,我可以加快速度。有没有办法做到这一点?

如果这不是我速度慢的原因,我还能如何加快速度?

更新:

我认为我的问题没有被理解。也许,列出我的源代码将有助于解释:

# ORF is the file extension of the files I want to move;
# These files live in dirs shared by JPEG files,
# which I do not want to move.
import os
import re
from glob import glob
import shutil

DIGITAL_NEGATIVES_DIR = ...
DATE_PATTERN = re.compile('\d{4}-\d\d-\d\d')

# Move a single ORF.
def move_orf(src):
    dir, fn = os.path.split(src)
    shutil.move(src, os.path.join('raw', dir))

# Move all ORFs in a single directory.
def move_orfs_from_dir(src):
    orfs = glob(os.path.join(src, '*.ORF'))
    if not orfs:
        return
    os.mkdir(os.path.join('raw', src))
    print 'Moving %3d ORF files from %s to raw dir.' % (len(orfs), src)
    for orf in orfs:
        move_orf(orf)

# Scan for dirs that contain ORFs that need to be moved, and move them.
def main():
    os.chdir(DIGITAL_NEGATIVES_DIR)
    src_dirs = filter(DATE_PATTERN.match, os.listdir(os.curdir))
    for dir in src_dirs:
        move_orfs_from_dir(dir)

if __name__ == '__main__':
    main()

【问题讨论】:

  • 你能提供你的脚本(或只是它的核心)吗?你用什么来复制或移动?
  • 我不想移动整个目录,尽管一个目录可能包含许多应该移动的文件。
  • 我正在使用 shutil.move 来移动单个文件
  • 嗯,应该很快。你是用os.walk遍历目录找文件吗?
  • @JoshD 我很确定我是(无权访问文件 atm)。

标签: python file move performance shutil


【解决方案1】:

你在哪个平台上?它真的必须是 Python 吗?如果没有,您可以简单地使用像 mv (*nix) 或 move (windows) 这样的系统工具。

$ stat -c "%s" file
382849574

$ time python -c 'import shutil;shutil.move("file","/tmp")'

real    0m29.698s
user    0m0.349s 
sys     0m1.862s 

$ time mv file /tmp

real    0m29.149s
user    0m0.011s 
sys     0m1.607s 

$ time python -c 'import shutil;shutil.move("file","/tmp")'

real    0m30.349s
user    0m0.349s 
sys     0m2.015s 

$ time mv file /tmp

real    0m28.292s
user    0m0.015s 
sys     0m1.702s 

$ cat test.py
#!/usr/bin/env python
import shutil
shutil.move("file","/tmp")
shutil.move("/tmp/file",".")

$ cat test.sh
#!/bin/bash
mv file /tmp
mv /tmp/file .

# time python test.py

real    1m1.175s
user    0m0.641s
sys     0m4.110s

$ time bash test.sh

real    1m1.040s
user    0m0.026s
sys     0m3.242s

$ time python test.py

real    1m3.348s
user    0m0.659s
sys     0m4.024s

$ time bash test.sh

real    1m1.740s
user    0m0.017s
sys     0m3.276s

【讨论】:

  • 没有什么比在 Python 中更快的原因了。它通常受 I/O 限制。
  • 在我的 linux 机器上的所有测试中,使用系统 mv 比 Python shutil.move 快。
  • @user131527:听起来他有一个脚本可以定位特定文件并移动它们。在那种情况下(因为他已经在 python 中了)shutil.move(stuff)os.system('mv stuff'); 写起来更干净、更安全一旦你已经在运行 python 解释器,区别是没有意义的,因为 shutil.move 只是调用系统的移动。
  • 这就是为什么我问是否必须使用 Python,对吧?如果没有,请使用 shell 的 mv 命令而不是 Python。
  • 我确信这可以在 shell 中完成(我假设它是图灵完备的),但我看不出为什么这在 Python 中应该很慢。
【解决方案2】:

编辑:

在我自己的困惑状态中(JoshD 帮助纠正了这一点),我忘记了 shutil.move 接受目录,因此您可以(并且应该)使用它来批量移动您的目录。

【讨论】:

  • 我认为他想搬家而不是复制……也许吧。在这种情况下,一个简单的移动比复制然后删除要快很多
  • 从技术上讲,“复制”应该比“移动”快。由于移动确实“复制+删除”。一种加快程序速度的方法。
  • @movieyoda:我认为您没有移动 20GB 目录然后复制了相同的 20GB 目录,是吗?移动(在同一个磁盘上)只是一个重命名。
【解决方案3】:

如果你只是想移动目录,你可以使用 shutil.move。它会非常快(如果它在同一个文件系统上),因为它只是一个重命名操作。

【讨论】:

  • 确切地说是在同一个文件系统上。
  • 哦,顺便说一句,shutil.move 会自动执行 try: os.rename(...) except OSError: ...copy and delete...,因此在 99% 的情况下没有理由使用 os.rename
  • @AndiDog:感谢您澄清这些细节。我会用更准确的信息更新答案。
猜你喜欢
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2021-08-02
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多