使用python 2.7 在学习文件操作时使用open()函数遇到报错

 python中的问题解决:‘encoding’ is an invalid keyword for this function

 使用python 3.7则无此问题

代码如下

f=open('exerice_4.py','a',encoding='utf-8')
f.write('1111111')

解决方案:在python2.7中,如果需要在open()函数中使用encoding,就需要引用io模块

代码修改为:

import io
f=io.open('exerice_4.py','a',encoding='utf-8')
f.write('1111111')

但是打印内容会有转义符....

 

相关文章:

  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
  • 2021-09-05
  • 2021-06-16
  • 2021-11-04
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2021-09-10
  • 2022-01-31
相关资源
相似解决方案