Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created March 6, 2013 05:29
Show Gist options
  • Save chenzx/5096961 to your computer and use it in GitHub Desktop.
Save chenzx/5096961 to your computer and use it in GitHub Desktop.
Copy directory tree according to a relative-path-list filter file
#!/usr/bin/env python
# encoding: utf-8
#
import os
import os.path
import sys
import shutil
def copy_directory_tree( base_dir, rel_path_list_file, to_dir ):
rel_path_list = [line.strip() for line in open(rel_path_list_file, 'r')]
for rel_path in rel_path_list:
print "Copying %s..." % (rel_path)
base_abspath = os.path.join(base_dir, rel_path)
to_abspath = os.path.join(to_dir, rel_path)
to_absdir = os.path.dirname(to_abspath)
if not os.path.exists(to_absdir):
os.makedirs(to_absdir)
shutil.copyfile(base_abspath, to_abspath)
def Main():
if len(sys.argv)!=4:
print 'Usage: python copy_directory_tree.py <base_dir> <rel_path_list_file> <to_dir>'
sys.exit(0)
else:
copy_directory_tree(sys.argv[1], sys.argv[2], sys.argv[3])
if __name__ == '__main__':
sys.exit(Main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment