I'm trying to remove all accents from a all coding files in a folder.. I already have success in building the list of files, the problem is that when I try to use unicodedata to normalize I get the error: ** Traceback (most recent call last): File "/usr/lib/gedit-2/plugins/pythonconsole/console.py", line 336, in __run exec command in self.namespace File "", line 2, in UnicodeDecodeError: 'utf8' codec can't decode byte 0xf3 in position 25: invalid continuation byte **
if options.remove_nonascii: nERROR = 0 print _("# Removing all acentuation from coding files in %s") % (options.folder) exts = ('.f90', '.f', '.cpp', '.c', '.hpp', '.h', '.py'); files=set() for dirpath, dirnames, filenames in os.walk(options.folder): for filename in (f for f in filenames if f.endswith(exts)): files.add(os.path.join(dirpath,filename)) for i in range(len(files)): f = files.pop() ; os.rename(f,f+'.BACK') with open(f,'w') as File: for line in open(f+'.BACK').readlines(): try: newLine = unicodedata.normalize('NFKD',unicode(line)).encode('ascii','ignore') File.write(newLine) except UnicodeDecodeError: nERROR +=1 print "ERROR n %i - Could not remove from Line: %i" % (nERROR,i) newLine = line File.write(newLine)