useful script, file renamer in python

Kristian Hermansen kristian.hermansen-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Mon May 28 02:49:24 EDT 2007


I came across this and thought some of you might find it useful.  It's
a python script to convert all files in a directory to sane file
names.  Sure, you could use any of rename, sed, awk, etc -- but I ran
into an issue tonight where I could not rename some files with
interesting characters in them.  Maybe it is bash's fault or maybe I
don't some encoding enabled, but I do know that Python didn't care!
Here it is.  It might be crap code and written by a noob, but it
worked for me :-)

Rename files in a folder with sane names

#!/usr/bin/python
"""
A script to make all the file names in the folder
changed according to certain rules:
    all small letters
    has only a-z, 0-9 and _
    only one period

vsbabu AT hotmail DOT com

I use it fairly often to rename files sent to me by
Microsoft Windows users, before uploading them to
any unix machine.

Unhandled error conditions include:
- no graceful processing if a target filename already exists.
"""
import re
import string
import os

def cleanup_foldername(strname):
    """cleans up the given folder name
    folder name should start and end with an alphabet
    and can have only a-z, 0-9 and _ in between
    """
    valid_dir = re.compile(r"^\w+$")
    invalid_stuff = re.compile(r"\W+")
    strname = string.lower(strname)
    strname = re.sub("^[^\w]+","",strname) #trim the beginning
    strname = re.sub("[^\w]+$","",strname) #trim the end
    strname = invalid_stuff.sub("_",string.strip(strname)) #replace
invalid stuff by _
    strname = re.sub("_+","_",strname) #squeeze continuous _ to one _
    if valid_dir.match(strname):
        return strname
    else:
        return ''

def cleanup_filename(strname):
    """cleans up for a file name
    file name rules are similar to folder names, but it
    can and must have one extension"""
    strname = string.lower(strname)
    (strfile, strext) = os.path.splitext(strname)
    strext = string.strip(strext)
    strfile = string.replace(strfile,'.','_')
    if strext == '' or strext == '.' or strext[0]!='.':
        return ''
    strfile = cleanup_foldername(strfile)
    if strfile == '':
        return ''
    strext = cleanup_foldername(strext[1:])
    if strext == '':
        return ''
    return strfile + '.' + strext

def  ren_file(dir,f):
    """rename the file according to the rules"""
    c=cleanup_filename(f)
    if (c != "" and c != f):
        print "%s -> %s\n" % (f,c)
        os.rename(dir+os.sep+f, dir+os.sep+c)
    return


def process_dir(dir):
    """process all files in the folder"""

    for f in os.listdir(dir):
        file = dir + os.sep + f
        if os.path.isdir(file):
            process_dir(file)
        else:
            ren_file(dir,f)

    return

def main():
    """main routine"""

    process_dir('.')
    return

if __name__=='__main__': main()
-- 
Kristian Hermansen

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.






More information about the Discuss mailing list