#!/usr/bin/env python3
====================================================================== - strip track numbers in all filenames in a folder.
License: provided freely, but with no warranties of any kind.
Author/copyright: 2011, M. Lutz (

rename files, dropping numeric digits at front (track/disc 
numbers, like "02 xxx.mp3", "02 - xxx.mp3", "3-02 xxx.mp3");
these make it difficult to detect duplicates by sorts, etc.;
run after N-dir merge via drag-and-drop, after normalizing 
a tree with;  python 3.X-only: input();
this version was polished slightly in May 2018 ('.*', verify);

use this script with care: it may change filenames in-place;
also set verify to False with care: any non-alpha prefixes in 
filenames will be silently stripped, even if the prefix is not 
a track number (e.g., "2112 Overture.mp3", "(What's...).mp3");

import os, string
verify = True   # False=don't ask about changes (see caution above)

cwd = input('Folder to scan? ')                       # this dir only (not subdirs)
for name in os.listdir('.'):
    if not os.path.isfile(name) or name[0] == '.':    # skip subdirs, ".*" hiddens
    if name[0] in string.ascii_letters:               # skip if alpha start already
    if verify and input('Rename? "%s" ' % name).lower() != 'y':

    rename = name
    while rename and rename[0] not in string.ascii_letters:   # scan to first alpha
        rename = rename[1:]
    if not rename:
        rename = name   # all nonalpha

    altname = verify and input('new name? ["%s"] ' % rename)  # Enter=[suggestion]
    if altname: 
        rename = altname

    if os.path.exists(rename):
        print('DUPLICATE SKIPPED: "%s" -> "%s"' % (name, rename))
        os.rename(name, rename)
        print('renamed: "%s" -> "%s"' % (name, rename))

Example run (see also

Folder to scan? C:\MusicMergeNov11\Flattened\Playable

Rename? "(What's So Funny 'Bout) Peace, Love.mp3"

Rename? "01 - Four Wheel Drive.mp3" y
new name? ["Four Wheel Drive.mp3"]
renamed: "01 - Four Wheel Drive.mp3" -> "Four Wheel Drive.mp3"

Rename? "01 - She Drives Me Crazy.mp3" y
new name? ["She Drives Me Crazy.mp3"] Hmmm.mp3
renamed: "01 - She Drives Me Crazy.mp3" -> "Hmmm.mp3"

Rename? "01 Always Look On the Bright Side of.m4a"

With verify=False (and on Mac OS):

/MY-STUFF/Code$ ./ 
Folder to scan? /Users/mini/Desktop/test/Flattened/Playable

renamed: "06 - State Of The Ark.mp3" -> "State Of The Ark.mp3"

renamed: "04 - The Lady in My Life.mp3" -> "The Lady in My Life.mp3"


[Home] Books Programs Blog Python Author Training Search Email ©M.Lutz