Fixing Filenames

I use SoundJuicer to rip my music CDs into Ogg files, and play them in Rhythmbox.

Both programs work great, with the exception of stripping special characters--like ',' '<' or '#'--from filenames. SoundJuicer has an option marked "Strip Special Characters," but it doesn't always work. Some special characters--anything with an accent mark above it, for instance--it won't ever strip. Since Rhythmbox chokes on filenames with special characters, you have to change the filename before it'll play the file.

This is tedious to do manually, so I wrote a Python script to do it for me. It's not very elegant, but it gets the job done: invoke it from the command line followed by a path to the directory containing the files you need stripped of special characters, and it takes care of the rest.

Here's the code:

#!/usr/bin/python
# This is a simple python program to look through a directory and
# strip special characters from the filenames in that directory. I use
# it mostly to fix filenames after importing music from CDs.

# Invoke this program from the command line followed by the path to the directory
# containing the filenames you want fixed.
#
# Copyright (C) 2008 Ron Toland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# long with this program. If not, see <[www.gnu.org/licenses/](http://www.gnu.org/licenses/)>.

import os, sys

def namefixer(dummy, dirname, filesindir):
for fname in filesindir:
newname = fname.replace(' ', '_')
newname2 = newname.replace(',', '')
newname3 = newname2.replace('+', '_')
newname4 = newname3.replace('-', '_')
newname5 = newname4.replace('#', 'no')
newname6 = newname5.replace('>', 'gt')
newname7 = newname6.replace('<', 'lt')
os.rename(os.path.join(dirname, fname), os.path.join(dirname, newname7))

if __name__ == '__main__':
os.path.walk(sys.argv[1], namefixer, None)

Save this file as "fixer.py" somewhere in your home directory, then use "chmod a+x fixer.py" to make it executable. To make it extra easy to use, create a link like this:

"sudo ln -s <full path to fixer.py> /usr/local/bin/namefixer"

That'll create a link file in your usr/local/bin folder that'll let you invoke the program from any directory just by typing: "namefixer <path to directory you want fixed>"

I'm still working on getting fixer.py to get rid of accented characters. When I figure it out, I'll post the corrected code. Should any of you, dear readers, get it working first, please let me know!

Ron Toland @mindbat