Skip to content

Instantly share code, notes, and snippets.

@PamelaM
Last active August 29, 2015 14:21
Show Gist options
  • Save PamelaM/17247d834b4ef7689ec1 to your computer and use it in GitHub Desktop.
Save PamelaM/17247d834b4ef7689ec1 to your computer and use it in GitHub Desktop.
http://www.pythoncentral.io/how-to-traverse-a-directory-tree-in-python-guide-to-os-walk/
import os
import shutil
input_directory = "FROM_BIG_DEPOSIT"
output_directory = "CLEAN_BIG_DEPOSIT"
def _src_2_dst(src_directory, src_name):
'''
examplish:
input = P-joke1-map.pdf
output = pReal1-map.pdf
'''
NAMES = [
("P-joke1", "pReal2"),
("P-joke", "pReal1"),
]
for old_name, new_name in NAMES:
if src_name.startswith(old_name):
dst_name = src_name.replace(old_name, new_name, 1)
return dst_name
else:
print "DID NOT FIND NEW NAME"
if os.path.exists(ouput_directory):
shutil.rmtree(ouput_directory)
os.mkdir(outout_directory)
for directory, subdirlist, filelist in os.walk(input_directory):
for fname in filelist:
src_path = os.path.join(directory, fname)
#dst_name = #.... some code to figure out what the new name should be ...
dst_name = _src_2_dst(directory, fname)
dst_path = os.path.join(output_directory, dst_name)
shutil.copy_file(src_path, dst_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment