Last active
April 20, 2017 11:55
-
-
Save Furkanzmc/5f070b72d0429fd74d08a84ffda6bca4 to your computer and use it in GitHub Desktop.
Python Script to Replace the File Names in a Folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Python Script to Replace the File Names in a Folder | |
# Changes by @m-etka | |
# | |
import os | |
# Get working directory | |
path = os.getcwd() | |
# Read desired input as string | |
before = raw_input("Replace From: ") | |
after = raw_input("Replace To: ") | |
# Loop files in the directory | |
for filename in os.listdir(path): | |
filename_without_ext = os.path.splitext(filename)[0] | |
extension = os.path.splitext(filename)[1] | |
# Find before text in filename, if exists change the value | |
if before in filename_without_ext: | |
new_file_name = filename_without_ext.replace(before, after) | |
new_file_name_with_ext = new_file_name + extension | |
os.rename( | |
os.path.join(path, filename), | |
os.path.join(path, new_file_name_with_ext) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I changed
input()
withraw_input()
in order to get rid of casting errors.Also changed path to working directory (Original one have problems with macOS).
Lastly, added a control for
before
text in filename.