Skip to content

Instantly share code, notes, and snippets.

@miratcan
Created December 15, 2012 13:05
Show Gist options
  • Save miratcan/4294784 to your computer and use it in GitHub Desktop.
Save miratcan/4294784 to your computer and use it in GitHub Desktop.
from __future__ import print_function
from os import mkdir
from os import walk
from os import popen
from os.path import join
from os.path import exists
from os.path import getsize
from os.path import basename
from datetime import datetime
from time import sleep
src_root = "/media/01CBAEC871225A00/"
dst_root = "/media/Seagate Expansion Drive/"
last_break = datetime.now()
MAX_CHUNK_SIZE = 10485760 # 10mb
BREAK_PERIODS = 60 # 10 min
BREAK_LENGTH = 30 # 5 min
# TODO: what happens on windows?
TERMINAL_WIDTH = int(popen('stty size', 'r').read().split()[1])
def sizeof(num):
# http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
for x in ['bytes','KB','MB','GB','TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
for path, folders, filenames in walk(src_root):
relative_path = path[len(src_root):]
dest_path = join(dst_root, relative_path)
if not exists(dest_path):
print(dest_path)
mkdir(dest_path)
for filename in filenames:
src_file_path = join(src_root, relative_path, filename)
dst_file_path = join(dst_root, relative_path, filename)
src_file = open(
join(src_root, relative_path, filename), 'rb')
dst_file = open(
join(dst_root, relative_path, filename), 'wb')
src_file_pos = src_file.tell()
src_file_length = getsize(src_file_path)
while src_file_pos < src_file_length:
chunk_size = min(MAX_CHUNK_SIZE, src_file_length - src_file_pos)
dst_file.write(src_file.read(chunk_size))
src_file_pos = src_file.tell()
current_time = datetime.now()
tdelta = current_time - last_break
if tdelta.seconds > BREAK_PERIODS:
print("\nI'm tired, taking break for %s Seconds" % BREAK_LENGTH)
sleep(BREAK_LENGTH)
last_break = current_time
print('copying: %s (%s/%s)' % (
basename(dst_file_path),
sizeof(src_file_pos),
sizeof(src_file_length)
), end="\r")
src_file.close()
dst_file.close()
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment