有没有办法在Python中快速移动许多文件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了有没有办法在Python中快速移动许多文件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2097字,纯文字阅读大概需要3分钟。
内容图文
![有没有办法在Python中快速移动许多文件?](/upload/InfoBanner/zyjiaocheng/807/e9a2123ab33f4883973319edebc19348.jpg)
我有一个小脚本可以在我的照片集中移动文件,但运行速度有点慢.
我想这是因为我一次只做一个文件移动.如果我同时将所有文件从一个目录移动到另一个目录,我猜我可以加快速度.有没有办法做到这一点?
如果这不是我缓慢的原因,我怎么能加快速度呢?
更新:
我认为我的问题不被理解.也许,列出我的源代码将有助于解释:
# 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()
解决方法:
你在什么平台上?它真的必须是Python吗?如果没有,您可以简单地使用系统工具,如mv(* nix)或移动(窗口).
$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中快速移动许多文件?全部内容,希望文章能够帮你解决有没有办法在Python中快速移动许多文件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。