Rename files with numbers

Micah Elliott mde at micah.elliott.name
Mon Oct 31 17:08:41 EST 2005


On Oct 31, dudufigueiredo at gmail.com wrote:
> but my focus is to learn how to acess a folder and rename all files in
> this folder.

This is a little more flexible than my last post, and it does the
whole job::

    #! /usr/bin/env python

    import glob, os, string

    def fix_ugly_song_names(songdir, band, spacerepl='_'):
        """Rename an ugly MP3 file name to a beautified shell-correct
        name.  Only works for files named like "03 song name.mp3".
        Use `spacerepl` to alter space replacement character.
        WARNING: no error-handling!
        """
        # Begin working in specified `songdir`.
        os.chdir(songdir)
        # Shell-unfriendly characters made into a string.
        badchars = ''.join( set(string.punctuation) - set('-_+.~') )
        # Identity table for later translation (removal of `badchars`).
        tbl = string.maketrans('', '')
        # MP3 files in `songdir` having ugly characters.
        uglies = glob.glob("*.mp3")
        # Make some step-by-step changes to build `pretties` list.
        pretties = []
        for ugly in uglies:
            song, ext = os.path.splitext(ugly)
            song = song.translate(tbl, badchars)
            song = song.replace(" ", spacerepl)
            song = song.title()
            song = band+spacerepl+"-"+spacerepl+song[3:]
            songext = song + ext
            pretties.append(songext)
        # Rename each file from ugly to pretty.
        for ugly, pretty in zip(uglies, pretties):
            if __debug__:
                print ugly, '-->', pretty
            else:
                os.rename(ugly, pretty)

So for you to use spaces, just call with something like this::

    fix_ugly_song_names('/var/mp3/pop/The_Beatles/Rubber_Soul',
                        'The Beatles',
                        spacerepl=' ')

I used the __debug__ gate to allow you to just see what it will do::

    $ ls -1
    01 drive my car.mp3
    02 norwegian wood.mp3
    03 you won't see me.mp3
    04 nowhere man.mp3
    mvmp3.py
    $ python ./mvmp3.py
    01 drive my car.mp3 --> The Beatles - Drive My Car.mp3
    02 norwegian wood.mp3 --> The Beatles - Norwegian Wood.mp3
    03 you won't see me.mp3 --> The Beatles - You Wont See Me.mp3
    04 nowhere man.mp3 --> The Beatles - Nowhere Man.mp3

And then to actually do it with the non-__debug__ path::

    $ python -O ./mvmp3.py
    $ ls -1
    mvmp3.py
    The Beatles - Drive My Car.mp3
    The Beatles - Norwegian Wood.mp3
    The Beatles - Nowhere Man.mp3
    The Beatles - You Wont See Me.mp3

If you want to keep the `badchars` in the file names, then you will
have to do *more* work since `song.title()` won't be able to do the
work for you.

Now I need to go beautify my collection.  :-)

-- 
_ _     ___
|V|icah |- lliott  http://micah.elliott.name  mde at micah.elliott.name
" "     """



More information about the Python-list mailing list