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

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

 

 使用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')

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

 

posted @ 2019-09-03 15:35  ReseeLei  阅读(4476)  评论(0编辑  收藏  举报