Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save openroomxyz/a57065812387db79205bb4fc75348170 to your computer and use it in GitHub Desktop.

Select an option

Save openroomxyz/a57065812387db79205bb4fc75348170 to your computer and use it in GitHub Desktop.
Python : How to rename all files in a folder so that their names are ordered numbers?
import os
def get_list_of_files_only_in_folder(folder_path):
return [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
def get_extension(name):
return name.split('.')[-1]
def rename_files_in_folder_to_numerical_order(path, start_index = 0):
n = start_index;
for i in get_list_of_files_only_in_folder(path):
old_path_to_file = path +"//"+ i
new_path_to_file = path + "//" + str(n) +"."+ get_extension(i)
os.rename(old_path_to_file, new_path_to_file)
n += 1
rename_files_in_folder_to_numerical_order("C://Users//X//Desktop//X//B", 0)
#Rename all files in folder so that thair names are ordered numbers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment