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))

由于文件涉及到windows的 CRLF 以及unix的LF问题,使用rb+会导致修改过的文件存在数据换行错误问题,使用wb则不会出现这样情况。具体原因待考证。

注:不建议使用此处的linecache 。使用linecache须在文件头注明文件编码格式:coding = utf-8

相关文章:

  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2022-01-24
猜你喜欢
  • 2021-09-14
  • 2021-07-01
  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
相关资源
相似解决方案