Skip to content

Instantly share code, notes, and snippets.

@liquiddandruff
Last active December 21, 2015 10:29
Show Gist options
  • Save liquiddandruff/6292693 to your computer and use it in GitHub Desktop.
Save liquiddandruff/6292693 to your computer and use it in GitHub Desktop.
Little script that tries to beautify and clean the names of messily named files in its current directory.
import msvcrt
import os
import sys
import string
currentDirectory = os.path.split(sys.argv[0])[0]
fileNames = [ f for f in os.listdir(currentDirectory) if os.path.isfile(os.path.join(currentDirectory,f)) ]
# Don't include the script
numFiles = len(fileNames) - 1
allowedPunc = '\'!'
'''
Determines how often a character appears at each
index position
'''
count = {}
for fileName in fileNames:
# Get rid of filetype
fileName = fileName[:fileName.rfind('.')]
index = 0
for char in fileName:
# Get the list of characters seen at this index
rangeCharsInCurrIndex = count.get(index, {})
# Increment the occurance of this char at this index
updatedCharCount = rangeCharsInCurrIndex.get(char, 0) + 1
rangeCharsInCurrIndex[char] = updatedCharCount
# Update count for this index
count[index] = rangeCharsInCurrIndex
index += 1
'''
Appends the characters which are found in all names
at the same index into a string
'''
print count
junk = ''
it = -1
for index in count:
for char in count[index]:
if count[index][char] == numFiles:
if it + 1 == index:
junk += char
it += 1
if junk:
print "Removing: " + junk + "\n"
else:
print "No common junk to remove. \n"
'''
1. Deletes junk
2. Removes the first 2 digits if there are any
3. Replace underlines with spaces
4. Capitalizes words
'''
newNames = {}
for fileName in fileNames:
if fileName[-2:] == 'py':
newNames[fileName] = fileName
continue
#1
newName = fileName.replace(junk, '')
#2
if fileName[0].isdigit() and fileName[1].isdigit():
newName = newName[2:]
#3
newName = newName.replace('_', ' ')
#4
cappedName = ''
currWord = ''
newName.capitalize()
for char in newName:
if char not in string.punctuation and char != ' ' or char in allowedPunc:
currWord += char
else:
if currWord:
cappedName += (currWord[0].upper() + currWord[1:]) + " "
currWord = ''
cappedName = cappedName[:-1]
# done
newName = cappedName + fileName[fileName.rfind('.'):]
newNames[fileName] = newName
print "OLD: " + fileName
print "NEW: " + newName + "\n"
print "\nProceed? y/n: "
ver = msvcrt.getch().lower()
if ver == 'y':
for fileName in fileNames:
rename(fileName, newNames[fileName])
elif ver == 'n':
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment