写真コレクション内のファイルを移動する小さなスクリプトがありますが、実行速度が少し遅くなります。
一度に1つのファイルを移動しているためだと思います。あるディレクトリから別のディレクトリへのすべてのファイル移動を同時に行うと、これを高速化できると思います。それを行う方法はありますか?
それが私の速度低下の理由でない場合、他にどのように速度を上げることができますか?
アップデート:
私の問題が理解されていないと思います。おそらく、私のソースコードをリストすると説明に役立つでしょう:
# 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()