File: rename-itunes.py

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-2018, M. Lutz (http://learning-python.com).
Versions: 
 2.0, Jul-2018
 Check for same content on duplicates-post-rename,
 and delete redundant copies automatically.
 1.1, May-2018 
 Polish slightly: '.*' skips, verify=False option.
 1.0. 2011
 Original version (named "renamer.py" till renamed...).
 
Renames all music files in a single folder, dropping any numeric 
digits at the front of their filenames to discard track/disc numbers
(e.g., "02 xxx.mp3", "02 - xxx.mp3", and "3-02 xxx.mp3"). These 
prefixes make it difficult to detect duplicates by sorts, etc.
Typically run against the Flattened/Playable folder produced by 
running flatten-itunes-2.py to normalize an entire music-file tree.
May also be run against a files folder created by manually merging
other folders via drag-and-drop or other. Python 3.X-only: input().
CAUTION: use this script with care - it changes filenames in-place,
with no provision for undoing its changes. Also note that its 
"verify" switch now False by default, so any non-alpha prefixes in 
filenames will be silently stripped, even prefixes that are not 
real track numbers (e.g., "2112 Overture.mp3", "90 degrees.mp3",
"(What's...).mp3"). Set verify=True to be asked about each rename,
and review its results in the results file and subject folder.
======================================================================
"""
import os, string, sys
verify = False # True=ask about every change (see caution above)
cwd = input('Folder to scan? ') # this dir only (not subdirs)
if not os.path.isdir(cwd):
 print('Invalid folder name: exiting.')
 sys.exit(1)
if not input('Will rename files in-place - proceed? ').startswith(('y', 'Y')):
 print('Run cancelled, no changes made.')
 sys.exit(1)
sys.stdout = open('Rename-results.txt', 'w') # send prints here (tbd: encoding)
os.chdir(cwd) # cd to folder - all files in '.'
numfiles = numrename = numdupskip = numdupkeep = 0
for name in os.listdir('.'): # scan preexisting filenames
 numfiles += 1
 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
 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('Target filename already exists: "%s" -> "%s"' % (name, rename)) 
 if open(name, 'rb').read() == open(rename, 'rb').read():
 numdupskip += 1
 print('\tFiles have same content - discarded redundant copy: "%s"' % name)
 os.remove(name)
 else:
 numdupkeep += 1
 print('\t***CONTENT DIFFERS - retained both original files')
 print('\tFile sizes:', os.path.getsize(name), os.path.getsize(rename))
 else: 
 numrename += 1
 os.rename(name, rename)
 print('Renamed: "%s" -> "%s"' % (name, rename))
print('\nSummary: files=%d, renames=%d, dupskips=%d, dupkeeps=%d.' % 
 (numfiles, numrename, numdupskip, numdupkeep))
sys.stderr.write('See results in file: %s\n' % 'Rename-results.txt')
"""
===================================================================================
EXAMPLE USAGE (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...
-----------------------------------------------------------------------------------
"""



AltStyle によって変換されたページ (->オリジナル) /