python怎么批量移动文件_有没有办法在Python中快速移动许多文件?

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

我想是因为我一次只能移动一个文件。我想如果我同时将所有文件从一个目录移动到另一个目录,我可以加快速度。有办法吗?

如果这不是我慢下来的原因,我怎么能加快速度呢?

更新:

我认为我的问题没有被理解。也许,列出我的源代码将有助于解释:# 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值