Mirror directory.py

From Colettapedia
Jump to navigation Jump to search
#!/usr/bin/env python

import os
import sys

source_dir = "image_sets"
target_dir = sys.argv[1]

source_abs_dir = os.path.abspath( source_dir )

print "Making target top level dir: ", target_dir
try:
	os.mkdir( target_dir )
except:
		print target_dir+" already exists"
for root, dirs, files in os.walk( source_dir ):
	print "current node: ", root
	try:
		tlsd, parent_dirs = root.split( os.sep, 1 )
	except ValueError:
		parent_dirs = ""
	print "\tdistance to top level: ", parent_dirs 
	for local_dir in dirs:
		new_dir = os.path.join( target_dir, parent_dirs, local_dir )
		print "\tmaking dir: ", new_dir
		try:
			os.mkdir( new_dir )
		except:
				print "\t\tAlready exists"
	for indiv_file in files:
		if indiv_file.endswith( ('.tif', '.tiff', '.TIF', '.TIFF') ):
			orig_path = os.path.join( source_abs_dir, parent_dirs, indiv_file )
			new_path = os.path.join( target_dir, parent_dirs, indiv_file )
			print orig_path, "--->", new_path
			try:
				os.symlink( orig_path, new_path )
			except:
				print "\t\tAlready exists"