Fix % filenames
I am one of those guys using "wget" to download things. Wget has this nasty habit of replacing special chars in files according to the URL escaping mechanisms. My favourite mp3-player Alsaplayer has or had a bug, where it didn't want to play files named with "%"s in it. As mp3-netlabels also have the nasty habit, to use special characters like "[thn001] some file.mp3" I end up with lots of files like: "%5Bthn001%5D%20some%20file.mp3" on my disk. Ugly, huh? It is.
So here's a little python script I use to rename such files in one go:
#!/usr/bin/python
import sys, os, urllib
dirs = sys.argv[1:]
if not dirs:
dirs = "."
for d in dirs:
if os.path.isdir(d):
print "Working on %s" % d
print "===========" + "="*len(d)
for f in os.listdir(d):
n = urllib.unquote(f)
if f != n:
f = os.path.join(d,f)
n = os.path.join(d,n)
if not os.path.exists(n):
try:
os.rename(f,n)
print "Moved: %s to %s" % (f,n)
except Exception, err:
print """Error:
Could not move %s to %s because "%s" """ % (f,n,err)
else:
print """WARNING: file "%s" already exists.
Move it out of the way first!""" % n
Save this as e.g. "fixnames.py" and call it from the command line as:
$ fixnames.py somedirectory /some/other/dir /even/more/dirs
If you omit the directory list, the current dir will be used silently.
comments
- unknown visitor wrote:
Thank you thank you thank you thank you! Had the same problem for years. :-)





