Skip to content

Instantly share code, notes, and snippets.

@noelyahan
Last active December 9, 2015 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noelyahan/8c6c3a9239469f0ecc97 to your computer and use it in GitHub Desktop.
Save noelyahan/8c6c3a9239469f0ecc97 to your computer and use it in GitHub Desktop.
# run instructions : python rename_all.py folder_path start_file_prefix
# ex: python rename_all.py /home/xxx/Pictures/my_collection "Garden-"
# output will be "Garden-1.png, Garden-2.png" etc..
import sys
import os
# input arguments folder_path & rename prefix
args = sys.argv
# folder path which files are contains
folder_path = args[1]
# rename prefix
rename_prefix = args[2]
# file names list
file_names = []
# preprocess variables
if folder_path[-1] != "/":
folder_path += "/"
# search for the files
for dir_path, sub_dir_list, file_list in os.walk(folder_path):
for fname in file_list:
# add file path to list for sorting purpose
file_names.append(fname)
# sort all the file names
file_names.sort()
# rename all
enum = 0
for f in file_names:
enum += 1
# organize the new name with the file extension
new_name = rename_prefix + str(enum) + f[f.index("."):]
# orgabnize the file name with full paths
old_file_name = folder_path + f
new_file_name = folder_path + new_name
#rename the file
os.rename(old_file_name, new_file_name)
print "----- RENAME PROCESS IS DONE !! ----"
print "+++ THANKS FOR USING THIS RENAME APP +++"
print ">>>> author @noelyahan <<<<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment