#File: purify.py #This should use Python3. import os #For tempory file stuff. from tempfile import mkstemp from shutil import move def maybe_delete_tilde(filename): if filename.endswith('~'): print('deleting ' + filename) os.remove(filename) return True return False def ok_byte(x, filename): if x == ord('\r'): print('removing \\r character from ' + filename) return False return True #Need to figure out the proper way to do #the next function. What I have cannot be the #best way. #x should be of integer type between 0 and 255 inclusive. def length_one_byte_string(x): return bytes(chr(x), encoding='UTF-8') def purify_file(filename): print('purifying file ' + filename) old_f = open(filename, 'rb') #Have a perfectly good file descriptor #in the variable fh, but heck with it. fh, abs_path = mkstemp() new_f = open(abs_path, 'wb') try: bs = old_f.read(1) while len(bs) == 1: x = bs[0] if x == ord('\r'): print('removing \\r character from ' + filename) elif x == ord('\t'): print('replacing \\t character with 4 spaces in ' + filename) for i in range(4): new_f.write(length_one_byte_string(32)) elif x > 127: print('WTF: removing character ' + chr(x) + ' (which is not ASCII)') else: #Doing the normal thing. new_f.write(length_one_byte_string(x)) bs = old_f.read(1) finally: new_f.close() os.close(fh) old_f.close() os.remove(filename) move(abs_path, filename) def handle_file(filename): #Deleting if ends with '~': if maybe_delete_tilde(filename): return if filename.endswith('.h') or \ filename.endswith('.cpp') or \ filename.endswith('.txt'): purify_file(filename) root_dir = '.' for dir_name, subdirs, files in os.walk(root_dir): for file in files: filename = dir_name + '/' + file handle_file(filename)