Return all files first before deleting the ".bak" in the code. I do not want you to lose something, or if my omissions are script, I would like you to be able to recreate what you have.
Secondly, this is probably not very good Python code, because I'm not an expert. But it works if you are editing in utf-8. Since en dash is not an ASCII character, direct replacement does not work. I admit, I'm not quite sure what is happening here, so larger python experts can figure out where I can do better.
#-*- coding: utf-8 -*- import codecs import glob import re import os def replace_file(file): endash = "–".encode('utf-8') print ("Replacing " + file) temp = codecs.open("temp", "w", "utf-8") with codecs.open(file) as f: for line in f: line = re.sub("-", "–", line) temp.write(line) temp.close() f.close() os.system("copy temp \"" + file + ".bak\"") x = glob.glob("*.tex") for y in x: replace_file(y)
source share