Skip to content

Instantly share code, notes, and snippets.

@geosem42
Created August 10, 2023 20:30
Show Gist options
  • Select an option

  • Save geosem42/e153c1ae43cf973c2a4ca48dea006e9b to your computer and use it in GitHub Desktop.

Select an option

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
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