Python 读写文件
时间:2010-10-20
来源:互联网
想要打开一个文件,既要读,也要写,就是先把文件内容读出来,然后往文件里写东西。
Python code
这样写。。。居然报错:
test
Traceback (most recent call last):
File "D:\Proj\PythonProj\test.py", line 7, in <module>
fileObj.write("what's up")
IOError: [Errno 0] Error
Python code
fileObj = open("test.ini","r+") content = fileObj.read() print content fileObj.write("what's up") fileObj.close()
这样写。。。居然报错:
test
Traceback (most recent call last):
File "D:\Proj\PythonProj\test.py", line 7, in <module>
fileObj.write("what's up")
IOError: [Errno 0] Error
作者: mid__night 发布时间: 2010-10-20
肯定报错的,你以读的方式打开,然后用这对象去写是不行的。
你要以写的方式打开再操作
Python code
写只能用写方式打开的对象
你要以写的方式打开再操作
Python code
fileObj_r = open("test.ini","r+") content = fileObj_r.read() fileObj_r.close() print content fileObj_w = open("test.ini","w+") fileObj_w.write("what's up") fileObj_w.close() #或者这样: content = open("test.ini","r+").read() print content fileObj_w = open("test.ini","w+") fileObj_w.write("what's up") fileObj_w.close()
写只能用写方式打开的对象
作者: amu9900 发布时间: 2010-10-20
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28