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?

Recommended Answers

All 3 Replies

The problem may be in the code below. In the first case, if user_input == '', you are passing a file name with the corresponding directory(s). In the second case, "else", you are passing the directory only, that was input by the user. This type of problem is generally a bad path, so break the code into pieces by commenting what you don't want, and print the file and path name everywhere you use it.

if __name__ == "__main__":
    print os.getcwd()
    if user_input == '':
        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 ]

        for track in filelist:
            rmtrcknum(track)

    else:
        rmtrcknum(user_input)

You perhaps want something like this but I am not sure what user_input or sys.argv is so you may have to adjust.

if __name__ == "__main__":
    print os.getcwd()
    path_name =user_input
    if user_input == '':
        ## print sys.argv and see what it prints
        path_name = sys.argv[1]   ## you want [1]

    for root, dir, files in os.walk(os.path.abspath(os.path.dirname(path_name))):
        filelist = [ os.path.join(root,fi) for fi in files ]

    for track in filelist:
        rmtrcknum(track)

Thanks a lot. That seems to make sense. I'll try it as soon as I get home.

Thanks a lot, I got it to work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.