Skip to content

Instantly share code, notes, and snippets.

@PathToLife
Last active January 9, 2021 16:57
Show Gist options
  • Save PathToLife/0669d9fe89a70d43f52edcccc0531c03 to your computer and use it in GitHub Desktop.
Save PathToLife/0669d9fe89a70d43f52edcccc0531c03 to your computer and use it in GitHub Desktop.
cleans anime file and folder names - python 3
import os
# https://gist.github.com/PathToLife/0669d9fe89a70d43f52edcccc0531c03
# cleans anime file and folder names
# OLD: [Something] Some Anime [1080p].mkv
# NEW: Some Anime [1080p].mkv
def clean_name(name):
name = name.strip()
if name[0] != '[':
return name
end_tab_idx = name.find(']')
if end_tab_idx == (len(name) - 1):
return name
producer_name = name[1:end_tab_idx]
new_name = name[end_tab_idx + 1:]
new_name = new_name.strip()
print(name, '->', new_name)
return new_name
def clean_folder_names(target_dir):
dir_list = os.listdir(target_dir)
folders_list = [x for x in dir_list if os.path.isdir(x)]
for folder_name in folders_list:
new_name = clean_name(folder_name)
if new_name == folder_name:
continue
src_fp = os.path.join(os.getcwd(), folder_name)
dest_fp = os.path.join(os.getcwd(), new_name)
os.rename(src_fp, dest_fp)
def clean_file_names(target_dir):
files = [x for x in os.listdir(target_dir) if os.path.isfile(os.path.join(target_dir, x))]
for current_name in files:
new_name = clean_name(current_name)
if new_name == current_name:
continue
src_fp = os.path.join(os.getcwd(), target_dir, current_name)
dest_fp = os.path.join(os.getcwd(), target_dir, new_name)
os.rename(src_fp, dest_fp)
clean_folder_names(os.getcwd())
clean_file_names(os.getcwd())
dirs = [x for x in os.listdir(os.getcwd()) if os.path.isdir(x)]
for d in dirs:
clean_file_names(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment