#!/usr/bin/env python import os, sys, shutil FORBIDDEN = ["?", "'", ":", "[", "]"] WS_CHAR = "_" VERBOSE = True def clean(filename): outfile = filename for frb in FORBIDDEN: outfile = outfile.replace(frb, "") outfile = outfile.replace(" ", WS_CHAR) return outfile def fix_names(directory): files = os.listdir(directory) outfiles = [clean(x) for x in files] files = [os.path.join(directory, x) for x in files] outfiles = [os.path.join(directory, x) for x in outfiles] for i in range(len(files)): if not files[i] == outfiles[i]: if VERBOSE: print("Moving: %s \n%8s%s\n") % (files[i], "=> ", outfiles[i]) shutil.move(files[i], outfiles[i]) else: if VERBOSE: print("No change needed for %s") % files[i] if __name__ == "__main__": if len(sys.argv) == 1: fix_names(".") else: if os.path.isdir(sys.argv[1]): fix_names(sys.argv[1]) else: print("USAGE: namefix