Skip to content

Instantly share code, notes, and snippets.

@bradwright
Created October 24, 2015 21:11
Show Gist options
  • Save bradwright/53fd3ce5a58843c1285f to your computer and use it in GitHub Desktop.
Save bradwright/53fd3ce5a58843c1285f to your computer and use it in GitHub Desktop.
Copy all files from one place to another, recursively, sleeping between runs
  1. Download the above file and save it somewhere (eg ~/Downloads)
  2. Open Terminal.app, and go to where it's saved (eg cd ~/Downloads)
  3. Run the script, with the HDD volume as the first argument, eg: python sleep-and-copy.py /Volumes/Thing/Path/to/itunes /Library/Local-path-to-itunes
#!/usr/bin/env python
import os
import sys
import time
import shutil
SLEEP_TIME = 3
def recursive_copy_and_sleep(source_folder, destination_folder):
"Copies files from `SOURCE` one at a time to `TARGET`, sleeping in between operations"
for root, dirs, files in os.walk(source_folder):
for item in files:
src_path = os.path.join(root, item)
dst_path = os.path.join(destination_folder, src_path.replace(source_folder, "")[1:])
time.sleep(SLEEP_TIME)
if os.path.exists(dst_path):
if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
print "Copying %s to %s" % (src_path, dst_path)
shutil.copy2(src_path, dst_path)
else:
print "Copying %s to %s" % (src_path, dst_path)
shutil.copy2(src_path, dst_path)
for item in dirs:
src_path = os.path.join(root, item)
dst_path = os.path.join(destination_folder, src_path.replace(source_folder, "")[1:]) # Lop off leading slash, otherwise the directory maker gets confused
if not os.path.exists(dst_path):
print "Making %s" % dst_path
os.mkdir(dst_path) #
return True
def main():
if len(sys.argv) != 3:
print "ERROR:\n\tPlease pass source and target directories, eg:\n\t%s /start/here /finish/here" % sys.argv[0]
sys.exit(1)
(source, target) = sys.argv[1:]
if os.path.exists(source) and os.path.exists(target):
recursive_copy_and_sleep(source, target)
sys.exit(0)
print "ERROR: invalid directories passed"
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment