#coding:utf-8
def test_file():
f=codecs.open('C:UsersjohnDesktop11.txt','r+',encoding='utf-8')
# content=f.read()
f.write(u"测试information")
f.close()
test_file()
错误提示:
"D:Program Files (x86)Python 2.7.11python.exe" "D:/Program Files (x86)/Python 2.7.11/Lib/site-packages/Card/algorithm/string_test.py"
Traceback (most recent call last):
File "D:/Program Files (x86)/Python 2.7.11/Lib/site-packages/Card/algorithm/string_test.py", line 100, in <module>
test_file()
File "D:/Program Files (x86)/Python 2.7.11/Lib/site-packages/Card/algorithm/string_test.py", line 80, in test_file
f=codecs.open('C:UsersjohnDesktop11.txt','r+',encoding='utf-8')
File "D:Program Files (x86)Python 2.7.11libcodecs.py", line 896, in open
file = __builtin__.open(filename, mode, buffering)
IOError: [Errno 22] invalid mode ('r+b') or filename: 'C:\Users\john\Desktop .txt'
也是被这编译器坑哭了,好几次的编译结果压根就不是当前的。
修改后的错误代码:
f=codecs.open('C:\Users\johnDesktop\11.txt','r+',encoding='utf-8')
-----------------------------------------
python中‘xx'编码格式为utf-8,形成了字符转义,必须用\,也可在字符串前加r,产生原始字符串即可。