Skip to content

Instantly share code, notes, and snippets.

@h3xstream
Created October 21, 2012 23:18
Show Gist options
  • Save h3xstream/3928893 to your computer and use it in GitHub Desktop.
Save h3xstream/3928893 to your computer and use it in GitHub Desktop.
Rename album cover to "folder.jpg"
#!/usr/bin/python
"""
Small script to rename image in folder to 'folder.jpg' to comply with XBMC default filename for music album.
Usage :
python folder.py /home/h3xstream/Music/
"""
import os
import sys
def processFolder(currentDir):
#print currentDir
try:
files = os.listdir(currentDir)
except:
print "?? An error occurs while listing file in ", currentDir
return
jpegs=[]
for file in files:
isFile = True
fullFile = os.path.join(currentDir, file)
isFile = os.path.isfile(fullFile)
if not(isFile): #recursive scan
processFolder(fullFile)
fileName, fileExtension = os.path.splitext(file)
#print fileName, fileExtension
if fileExtension in ['.jpg', '.jpeg'] :
jpegs.append(file)
#print jpegs
if "folder.jpg" in jpegs :
#print "OK"
pass
elif len(jpegs) == 1 and not jpegs[0].endswith("folder.jpg"):
#Renaming the filename
for jpgFile in jpegs:
srcFile = os.path.join(currentDir, jpgFile)
destFile = os.path.join(currentDir,"folder.jpg")
print "Renaming [", srcFile , "] to [", destFile, "]"
try :
os.rename(srcFile,destFile)
except WindowsError:
print "?? Unable to rename ", srcFile
elif len(jpegs) > 1:
#Ambiguous folder with multiple jpegs are skipped
print "!! Many potential covers in ", currentDir
pass
rootdir = sys.argv[1]
processFolder(rootdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment