File: rename-itunes.py

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

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 flatten-itunes-2.py;  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)
os.chdir(cwd)
for name in os.listdir('.'):
    if not os.path.isfile(name) or name[0] == '.':    # skip subdirs, ".*" hiddens
        continue
    if name[0] in string.ascii_letters:               # skip if alpha start already
        continue
    print()
    if verify and input('Rename? "%s" ' % name).lower() != 'y':
        print('skipped')
        continue

    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))
    else: 
        os.rename(name, rename)
        print('renamed: "%s" -> "%s"' % (name, rename))


"""
-----------------------------------------------------------------------------------
Example run (see also flatten-itunes-2.py):

E:\Music\resolve-itunes-nov11> rename-itunes.py
Folder to scan? C:\MusicMergeNov11\Flattened\Playable

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

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"
skipped

...etc...
-----------------------------------------------------------------------------------
With verify=False (and on Mac OS):

/MY-STUFF/Code$ ./rename-itunes.py 
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"

...etc...
-----------------------------------------------------------------------------------
"""



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