Created
August 10, 2023 20:30
-
-
Save geosem42/e153c1ae43cf973c2a4ca48dea006e9b to your computer and use it in GitHub Desktop.
Rename files in a directory if the filename includes an underscore, has a certain extension and replace it with a space
This file contains hidden or 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
| import os | |
| def rename_files(directory): | |
| for filename in os.listdir(directory): | |
| if "_" in filename and filename.endswith(".mkv"): | |
| new_filename = filename.replace("_", " ") | |
| src = os.path.join(directory, filename) | |
| dst = os.path.join(directory, new_filename) | |
| os.rename(src, dst) | |
| # Example usage: | |
| rename_files("D:\Series\Stargate\SG-1\Season 1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment