1 def change_file_code(coding,files_name): 2 try: 3 cache_data = linecache.getlines(files_name) 4 with open(files_name, 'wb') as out_file: 5 for line in range(len(cache_data)): 6 out_file.write(cache_data[line].encode(coding)) 7 except Exception as e: 8 print(str(e))
The relevant document to the windows of CRLF and LF issue unix using rb + can cause problems there is a data error wrap modified files, use the wb is not such a case. Specific reasons to be verified.
Note: not recommended linecache here. Use linecache shall indicate in the header file encoding format: coding = utf-8