import arcpy import os def make_filename_dictionary(name_table): """ Create a dictionary of filenames with newname and oldname. """ with arcpy.da.SearchCursor(name_table, ['NAME_NEW', 'NAME_CUR']) as rows: filename_dictionary = dict() for row in rows: filename_dictionary[str(row[1])] = str(row[0]) return filename_dictionary def get_new_filename(oldfilename, filename_dictionary): """ Use the old name to check for an updated name in the dictionary. """ if oldfilename in filename_dictionary.keys(): print "Match found: {0}, {1}".format(oldfilename, filename_dictionary[oldfilename]) return filename_dictionary[oldfilename] print "Name not found: {0}".format(oldfilename) return False def rename_files(directory, name_table): """ Rename the files in the input directory. """ new_file_names = make_filename_dictionary(name_table) old_files = [fi for fi in os.listdir(directory) if os.path.splitext(fi)[1] == ".pdf"] for f in old_files: new_file_name = get_new_filename(f, new_file_names) if new_file_name: old_file_path = os.path.join(directory, f) new_file_path = os.path.join(directory, new_file_name) os.rename(old_file_path, new_file_path) if __name__ == '__main__': d = # directory with pdf's n = # table with old names and new names rename_files(d, n)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question