Skip to content

Instantly share code, notes, and snippets.

@jpstacey
Created November 28, 2016 19:34
Show Gist options
  • Save jpstacey/7a4c27252120f74cc9c45e9ba30e9168 to your computer and use it in GitHub Desktop.
Save jpstacey/7a4c27252120f74cc9c45e9ba30e9168 to your computer and use it in GitHub Desktop.
Copy a file, preserving all directories, with a horrible mixture of python shutil and os.system('mkdir -p')
#!/usr/bin/env python
#
# Usage: copy_with_dirs.py <file> [file] [...]
# OR ... | xargs -d '\n' /tmp/copy_with_dirs.py
import shutil
import urllib
import sys
import os
for filename in sys.argv[2:]:
filename = urllib.unquote(filename)
print filename
new_filename = filename.replace(
'/home/jp/Music/library/',
'/media/jp/CHEWINGGUM/Kodi/'
)
os.system('mkdir -p "%s"' % os.path.dirname(new_filename))
shutil.copyfile(filename, new_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment