File: class/Extras/Other/_old-Tools/packaging/Scripts/fixnames_all.py

#########################################################
# Use: "python ..\..\Tools\fixnames_all.py".
# find all files with all upper-case names at 
# and below the current directory ('.'); for each, 
# ask the user for a new name to rename the file to;
# I use this script to catch old uppercase file
# names created on MS-DOS (case matters on some
# platforms, when importing Python module files);
# notes: 
# - the os.path.walk function may be better here,
#   since we visit every file: see fixnames_all2.py;
# - we could instead blindly covert all-upper names 
#   to all-lower too, but that's a bit dangerous so
#   convertOne suggests an all-lower default name;
# - this may fail on some machines if directory 
#   names are converted before their contents (the
#   original dir name in the path returned by find
#   no longer exists), but I've only run this on 
#   Dos/Windows where case doesn't matter in the 
#   directory path, for os.rename calls (ymmv);
# - the allUpper function's heuristic fails for odd
#   filenames that are all non-alphabetic (ex: '.');
#########################################################

import os, string
listonly = 0

def allUpper(name):
    for char in name:
        if char in string.lowercase:    # any lowercase letter disqualifies
            return 0                    # else all upper, digit, or special 
    return 1 

def convertOne(fname):
    fpath, oldfname = os.path.split(fname)
    if allUpper(oldfname):
        prompt = 'Convert dir=%s file=%s? (y|Y)' % (fpath, oldfname)
        if raw_input(prompt) in ['Y', 'y']:
            default  = string.lower(oldfname)
            newfname = raw_input('Type new file name (enter=%s): ' % default)
            newfname = newfname or default
            newfpath = os.path.join(fpath, newfname)
            os.rename(fname, newfpath)
            print 'Renamed:', fname, 'to', str(newfpath)
            raw_input('Press enter to continue')
            return 1
    return 0

if __name__ == '__main__':
    patts = "*"                              # inspect all file names
    from fixeoln_all import findFiles        # reuse finder function
    matches = findFiles(patts)

    ccount = vcount = 0
    for matchlist in matches:                # list of lists, one per pattern
        for fname in matchlist:              # fnames are full directory paths
            print vcount+1, '=>', fname      # includes names of directories 
            if not listonly:  
                ccount = ccount + convertOne(fname)
            vcount = vcount + 1
    print 'Converted %d files, visited %d' % (ccount, vcount)



[Home page] Books Code Blog Python Author Train Find ©M.Lutz