Skip to content

Instantly share code, notes, and snippets.

@paulwinex
Created January 31, 2019 15:43
Show Gist options
  • Save paulwinex/bd5ba5b16b836c6d029f2ca664d353c1 to your computer and use it in GitHub Desktop.
Save paulwinex/bd5ba5b16b836c6d029f2ca664d353c1 to your computer and use it in GitHub Desktop.
def copyfileobj(fsrc, fdst, callback, length=16*1024):
if os.path.exists(fdst):
if raw_input('File exists, {} \nOvervrite old? [y/n]: '.format(fdst)) == 'y':
print 'Remove file'
else:
print 'Skip copy'
return
copied = 0
prcnt = 0
size = os.path.getsize(fsrc)
print 'Copy file [{}]...'.format(sizeof_fmt(size))
fsrc = open(fsrc, 'rb')
fdst = open(fdst, 'wb')
_exit = False
while True:
buf = fsrc.read(length)
if not buf:
_exit = True
fdst.write(buf)
copied += len(buf)
p = int((float(copied) / size) * 100)
if p != prcnt:
callback(p)
prcnt = p
if _exit:
fsrc.close()
fdst.close()
break
def clb(x):
sys.stdout.write('\r{}% [{}{}]{}'.format(x, '#'*x, ' '*(100-x), '\n' if x == 100 else ''))
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment