Skip to content

Instantly share code, notes, and snippets.

@dehamzah
Created August 31, 2016 12:06
Show Gist options
  • Save dehamzah/70c3356399493597a10e9fb60b154fa7 to your computer and use it in GitHub Desktop.
Save dehamzah/70c3356399493597a10e9fb60b154fa7 to your computer and use it in GitHub Desktop.
Renaming bulk files by removing any number in file names.
#!/usr/bin/python
# Program for remove number from file names in targeted folder
import os
# Set target directory where the files resides
target_dir = r"/your/path/to/folder/here"
'''
r stands for rawpack. it means to tell python take this string as it is.
it will benefit you if you are in windows environment.
learn more: http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l
'''
def rename_files():
# Save current directory path
saved_path = os.getcwd()
# Change to target directory where the files resides
os.chdir(target_dir)
# Get all file names
file_lists = os.listdir(target_dir)
# Iterate each file
for file_name in file_lists:
# Remove any number from file_name
new_name = file_name.translate(None, '0123456789')
# Rename the file
os.rename(file_name, new_name)
print "File renamed: %s -> %s" % (file_name, new_name)
# Back to previous directory
os.chdir(saved_path)
# Run the function
rename_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment