Skip to content

Instantly share code, notes, and snippets.

@sgraaf
Last active December 4, 2017 21:07
Show Gist options
  • Save sgraaf/e2f1d3c44f7104cd784007dcf6fbc7c4 to your computer and use it in GitHub Desktop.
Save sgraaf/e2f1d3c44f7104cd784007dcf6fbc7c4 to your computer and use it in GitHub Desktop.
A simple script that auto-renames my chosen files and folders (after having downloaded them from bandcamp) to standardize them for my music library
import os
import mutagen
import sys
# function to find nth occurence of needle in haystack
def find_nth(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
# function to validate the user input
def choice_validation(choice, list_len):
if choice == 'q' or choice == 'Q':
sys.exit()
try:
choice = int(choice)
except:
raise ValueError('An integer was expected. Please make sure to enter an integer as input.')
if choice < 0 or choice > list_len:
raise ValueError('Please restrict your choice to an integer between 0 and ' + str(list_len))
else:
return(choice)
# function to create list of subdirectories of chosen directory
def list_directories(root):
dir_list = []
for dir in os.listdir(root):
if os.path.isdir(os.path.join(root, dir)):
dir_list.append(dir)
return(dir_list)
# function to print the list of subdirectories of chosen directory as a menu
def print_directories(dir_list):
print('\nIn the Downloads directory, we found the following subdirectories:')
for i in range(len(dir_list)):
print('[' + str(i) + ']:\t' + dir_list[i])
print('[' + str(len(dir_list)) + ']:\tAll')
print('[Q]\tExit')
# function to rename the files
def rename_files(path):
for filename in os.listdir(path):
if find_nth(filename, ' - ', 2) >= 0:
os.rename(os.path.join(path, filename), os.path.join(path, filename[find_nth(filename, ' - ', 2) + 3:]))
else:
continue
# function to rename the directories
def rename_dir(path):
audiofile = [afile for afile in os.listdir(path) if afile.startswith('01')][0]
audiomut = mutagen.File(os.path.join(path, audiofile))
# get the year of album release
try: # for flac
year = audiomut['date'][0]
except KeyError: # for mp3
year = audiomut['TDRC'][0]
# get the extension
ext = os.path.splitext(audiofile)[1][1:].upper()
if ext == 'MP3':
if audiomut.info.bitrate / 1000 == 320:
ext = '320'
else:
ext = 'V0'
# rename the directory accordingly
if path.find('(') >= 0:
new_path = path[:len(path) - (len(path) - path.find('('))] + ' (' + str(year) + ') [WEB ' + ext + ']'
else:
new_path = path + ' (' + str(year) + ') [WEB ' + ext.upper() + ']'
os.rename(path, new_path)
root = r'C:\Users\NAME\Downloads\'
dir_list = list_directories(root)
print_directories(dir_list)
choice = choice_validation(input('\nWhich (sub)directory do you want to run this renaming script on?: '), len(dir_list))
if choice == len(dir_list):
for dirr in dir_list:
rename_files(os.path.join(root, dirr))
rename_dir(os.path.join(root, dirr))
else:
rename_files(os.path.join(root, dir_list[choice]))
rename_dir(os.path.join(root, dir_list[choice]))
print('\nDone.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment