Old thread, but may help future readers ...
I would avoid using .lower () in file names, if only to make your code more platform independent. (linux is with touch, .lower () in the file name will surely ruin your logic in the end ... or, even worse, an important file!)
Why not use re ? (Although, to be even more reliable, you should check the header of the magic file for each file ... How to check the file type without extensions in python? )
import re def checkext(fname): if re.search('\.mp3$',fname,flags=re.IGNORECASE): return('mp3') if re.search('\.flac$',fname,flags=re.IGNORECASE): return('flac') return('skip') flist = ['myfile.mp3', 'myfile.MP3','myfile.mP3','myfile.mp4','myfile.flack','myfile.FLAC', 'myfile.Mov','myfile.fLaC'] for f in flist: print "{} ==> {}".format(f,checkext(f))
Output:
myfile.mp3 ==> mp3 myfile.MP3 ==> mp3 myfile.mP3 ==> mp3 myfile.mp4 ==> skip myfile.flack ==> skip myfile.FLAC ==> flac myfile.Mov ==> skip myfile.fLaC ==> flac
Dan F. Dec 15 '17 at 6:08 2017-12-15 06:08
source share