#!/usr/bin/env python # -*- coding: utf-8 -*- # """ Simple trick to rename all files to be compliant with FAT32 filesystems Usefull if you like to copy your files from a UFS/EXT filesystem to a FAT filesystem. By default it will dryrun to avoid (major) disasters. Version: $Id$ License: BSDLike http://rickvanderzwet.nl/LICENSE Rick van der Zwet """ import re import os import sys import string import argparse parser = argparse.ArgumentParser(description=__doc__,formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-r', '--rename', dest="dryrun", action='store_false',help="Rename the files",default=True) parser.add_argument('directories', nargs='+', metavar='DIR', type=str) args = parser.parse_args() trans_table = { u'é' : 'e', u'&' : 'and', u'Ø' : 'O', u'ü' : 'u', u'’' : "'", u'ö' : 'o', u'á' : 'a', } word_table = { "'m" : ' am', "'d" : ' would', "n't" : ' not', "n's" : 'n his', "'n'" : 'and', "n'" : 'ng', "'s" : ' is', "'ll" : ' will', "A'k" : "Als ik", "'T" : "Het", "'t" : "het", } replace_re = re.compile('[^a-zA-Z0-9\-\.\ ]+') def make_ascii(source): source = source.decode('utf-8') dest = "" for i in source: if trans_table.has_key(i): dest += trans_table[i] else: dest += i for key,value in word_table.iteritems(): dest = dest.replace(key,value) # Debug decoding # print "---" # print "Original : ", source, # for c in source: print ord(c), # print "\nConverted: ", dest, # for c in dest: print ord(c), # print "\n" dest = replace_re.sub('', dest) return str(dest) for root, dirs, files in os.walk(sys.argv[1],topdown=False): for file in files + dirs: new_file = make_ascii(file) if new_file != file: source = os.path.join(root,file) target = os.path.join(root,new_file) prefix = '# (dryrun) ' if args.dryrun else '' print prefix, "Source:", source print prefix, "Target:", target if not args.dryrun: os.rename(source,target)