I am making a script which manipulates ID3 tags. I want a certain function to be used on every file in a directory recursively. I have written the script but it is not behaving like I would expect in some cases.
If I give it exec perms and put it in /usr/bin and run 'script.py', the walk function doesnt seem to do its job properly and no files are passed to my function at all. If I try to run the script (which is in my cwd) in a directory filled with other directories, only one of the folders gets passed to my function. I don't think you will understand what I mean until you try the script. I have verified that all the parts work when run independently but they dont work well as a script...
Oh an btw I am using mutagen which you can get from http://code.google.com/p/mutagen/
#!/usr/bin/python
import mutagen.oggvorbis, mutagen.mp3, os, sys, re
user_input = raw_input("Please enter a file path.\n")
def rmtrcknum(trck):
fileogg = re.compile(".ogg$")
filemp3 = re.compile(".mp3$")
pattern = re.compile("^\d+ - ")
if fileogg.search(trck) == None:
if filemp3.search(trck) == None:
pass
else:
track = mutagen.mp3.EasyMP3(trck)
track['title'] = pattern.sub("", track['title'][0])
print track.pprint()
track.save()
os.rename(trck, os.path.abspath(os.path.dirname(trck)) + '/' + track['title'][0] + '.mp3')
else:
track = mutagen.oggvorbis.Open(trck)
track['title'] = pattern.sub("", track['title'][0])
print track.pprint()
track.save()
os.rename(trck, os.path.abspath(os.path.dirname(trck)) + '/' + track['title'][0] + '.ogg')
if __name__ == "__main__":
print os.getcwd()
for root, dir, files in os.walk(os.path.abspath(os.path.dirname(sys.argv[0]))):
filelist = [ os.path.join(root,fi) for fi in files ]
if user_input == '':
for track in filelist:
rmtrcknum(track)
else:
rmtrcknum(user_input)
Any ideas?