Recently I have had to do a few tasks that would have required a lot of man hours renaming files and creating xml documents.  First step was to remove any unwanted characters such as slashes quotes and so on.

print "Python script to clean up filenames removing any characters "  
print "we do not want"  
print "19 / 05 / 2009"  
print "Arthur: K. Aning"  
print " ------------------------------------- "  
print  
import os, time  
unwantedchars = ("/","\","'",""") #you can always add to this list  
directory = raw_input("What directory [eg: 'c:\path\to\files']: ")  
replacement = raw_input("Replacement Character? [default = ''] ")  
for root, dirpath, fname in os.walk(directory):  
    for files in fname:
        filepath = root+"\"+files
        for char in unwantedchars:
            newfilename = files.replace(char, replacement)
        if(os.path.exists(filepath)):
            print "Renaming: "+newfilename
            #os.rename(filepath, newfilename)
            print "--------------------------------------------"
        else:
            print "did not find file: "+filepath
            print "skipping"
            time.sleep(0.5)
Tags: