Question

I'm using Python 2.7 on a windows computer. I would like to know how the bitrate of my transfers copies. I have a list of sources and a list of destinations and call them in a for loop. The actual copy is done by the code below.

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

Is there a way to get the speed of my copy?

Thanks Gavin

Était-ce utile?

La solution

Unless the copy command gives feedback via stdout about the bitrate, you will need an external python library that does this for you OR you do it manually by checking the size of the new file every second and do some math.

Autres conseils

You could do something along these lines:

#!/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'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top