Skip to content

Instantly share code, notes, and snippets.

@WittmannF
Last active November 21, 2018 22:07
Show Gist options
  • Save WittmannF/3d227974902068b61c13cb5b33cb6a13 to your computer and use it in GitHub Desktop.
Save WittmannF/3d227974902068b61c13cb5b33cb6a13 to your computer and use it in GitHub Desktop.
Rename subtitles from Udacity class. Code by Bizelli.
import os
def rename_dir(path,sufix,extension_searched):
for file_name in os.listdir(path):
file_name_without_ext = os.path.splitext(file_name)[0]
extension = os.path.splitext(file_name)[1]
if extension == extension_searched:
if file_name_without_ext.rfind(sufix) > 0:
new_file_name = file_name[:file_name_without_ext.rfind(sufix)] + extension_searched
os.rename(os.path.join(path,file_name),os.path.join(path,new_file_name))
else:
os.chdir(path)
os.remove(file_name)
"""
The r means that the string is to be treated as a raw string, which means all escape codes will be ignored.
For an example:
'\n' will be treated as a newline character, while r'\n' will be treated as the characters \ followed by n.
"""
path = r"C:\Users\Bizelli\Google Drive\udacity\module_2\class_2"
sufix = " - lang_en_vs1"
extension = ".srt"
rename_dir(path,sufix,extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment