1

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

Gavin Hinfey
  • 279
  • 1
  • 2
  • 13

2 Answers2

0

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.

Nico
  • 6,395
  • 4
  • 25
  • 34
  • So I've tried to do this by checking the destination file size. `code` def work(): threading.Timer(0.25,work).start(); x = os.path.getsize(frame.worker.dst_abs_path) wx.CallAfter(frame.getspeed.SetLabel, str(x)) work() `code` However this does not update the file size as the file size is being copied. It give me the finished figure only. If I could find a way to find the size as the file is being written I'd be sorted. – Gavin Hinfey Oct 30 '13 at 10:03
  • Sorry for the formatting there. Anyone know how to format code in this comment section? – Gavin Hinfey Oct 30 '13 at 10:12
  • `code` def work(): threading.Timer(0.25,work).start(); x = os.path.getsize(frame.worker.dst_abs_path) wx.CallAfter(frame.getspeed.SetLabel, str(x)) work() `code` used in conjunction with shutil worked for me thanks – Gavin Hinfey Oct 30 '13 at 14:59
0

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'
dawg
  • 98,345
  • 23
  • 131
  • 206