我有一个小脚本,可以在我的照片集中移动文件,但运行速度有点慢。
我想是因为我一次只能移动一个文件。我想如果我同时将所有文件从一个目录移动到另一个目录,我可以加快速度。有办法吗?
如果这不是我慢下来的原因,我怎么能加快速度呢?
更新:
我认为我的问题没有被理解。也许,列出我的源代码将有助于解释:
# 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()
如果只想移动目录,可以使用shuil.move。它会非常快(如果它在同一个文件系统上的话),因为它只是一个重命名操作。
编辑:
在我自己的混乱状态下(JoshD帮了我一把),我忘记了
shutil.move
接受目录,所以您可以(而且应该)使用它将目录作为批处理移动。你在哪个站台?它真的必须是Python吗?如果没有,您可以简单地使用诸如
mv
(*nix)或move
(windows)之类的系统工具。相关问题 更多 >
编程相关推荐