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:

# 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
# 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 <;.

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 “” somewhere in your home directory, then use “chmod a+x” to make it executable. To make it extra easy to use, create a link like this:

“sudo ln -s <full path to> /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 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!

3 thoughts on “Fixing Filenames

  1. Do you have a device which doesn’t handle accented characters, or do you want á changed to a for other reasons?

    It would be fairly simple to extend the “strip special characters” to remove accents if you have a good argument for it.

  2. Oh, and if there are any characters which SJ doesn’t strip when you’ve checked Strip Special Characters and again, you have a good argument for it, file a bug report.

  3. Rhythmbox won’t play a file with accented characters in the filename. Otherwise what I have now would work fine for everything else I do.

    SoundJuicer doesn’t strip any of the special characters I’ve put in the above program: commas, less than or greater than signs, ‘#’ signs, accented characters, etc. Since they show up more often in music files than I thought at first, I’ll take your suggestion and file a bug report.

Comments are closed.