Kjetil's Information Center: A Blog About My Projects

Filename Sanitizer

Here is a Python script to sanitize filenames that be transferred to a Windows file system. This script will recursively go through a directory and replace the bad characters with underscores. Not sure if this script knows about all the bad ones, but it worked in my case at least.

Check it out:

#!/usr/bin/python

import os
import sys

bad_characters = r'?<>\:*|"'
replacement = '_'

if len(sys.argv) < 2:
    print "Usage: %s <directory> [yes]" % (sys.argv[0])
    sys.exit(1)

do_it = False
if len(sys.argv) > 2:
    if sys.argv[2] == 'yes':
        do_it = True

for directory, dirnames, filenames in os.walk(sys.argv[1], topdown=False):
    for name in filenames + dirnames:
        newname = name
        for bad in bad_characters:
            newname = newname.replace(bad, replacement)
        if newname != name:
            oldpath = os.path.join(directory, name)
            newpath = os.path.join(directory, newname)
            print "%s -> %s" % (oldpath, newpath)
            if do_it:
                os.rename(oldpath, newpath)
          


Topic: Scripts and Code, by Kjetil @ 06/02-2015, Article Link