0

我在 Windows 计算机上使用 Python 2.7。我想知道我的传输比特率是如何复制的。我有一个源列表和一个目的地列表,并在 for 循环中调用它们。实际复制由下面的代码完成。

subprocess.call(""" copy  "%s" "%s"  """ % (src_abs_path, dst_abs_path), shell=True)

有没有办法提高我的复制速度?

谢谢加文

4

2 回答 2

0

除非该copy命令通过标准输出提供有关比特率的反馈,否则您将需要一个外部 python 库来为您执行此操作,或者您通过每秒检查新文件的大小并进行一些数学运算来手动执行此操作。

于 2013-10-29T16:55:20.083 回答
0

您可以按照以下方式做一些事情:

#!/usr/bin/python
import sys
import os
import glob

class ProgBar(object):
    def __init__(self, **kwargs):
        self.length=kwargs.get('width', 40)
        self.header=kwargs.get('header', None)
        self.progress=0.0
        self.fmt=kwargs.get('fmt', "[{}] {:6.1%}")

    def start(self, prog=0.0):
        self.progress=prog
        if self.header:
            sys.stdout.write(self.header)

        return self

    def update(self, prog):
        sys.stdout.write('\r')
        if prog>1 or prog<0: raise ValueError('progress outside range')
        self.progress=prog
        block=int(round(self.length*prog))  
        txt=self.fmt.format("#"*block + "-"*(self.length-block), prog)
        self.outlen=len(txt)   
        sys.stdout.write(txt)
        sys.stdout.flush()        

def read_in_chunks(infile, chunk_size=1024):
    while True:
        chunk = infile.read(chunk_size)
        if chunk:
            yield chunk
        else:
            return        

src_path='/tmp' 
dest_path='/tmp/test_dir/1/2/3/4/5'
buf=10                               # artificially small...
os.chdir(src_path)

for file in glob.glob('*.txt'):
    src=os.path.join(src_path,file)
    dst=os.path.join(dest_path,file)
    src_size=os.path.getsize(file)
    with open(src, 'rb') as fin, open(dst, 'w') as fout:
        pb=ProgBar().start()
        print '{}\n=>\n{}'.format(src,dst)
        for i, chunk in enumerate(read_in_chunks(fin, buf), 1):
            fout.write(chunk)
            pb.update(float(i)*buf/src_size)
        print '\ndone'
于 2013-10-29T23:28:29.417 回答