python 文件处理模块使用,对一个文件追加写入
2015-10-28 02:35
495 查看
-----------------------------首先将已有文件读入内存中-----------------------------------
>>> name = file("name.txt","a") //(将name.txt文件读入到内存中,且允许对原文件追加写入 a表示允许追加写入 )
-----------------------------写入新内容到name.txt文件中--------------------------------
>>> name.write("hello 3")
-----------------------------将文件从内存中删除,并且保存到硬盘中-----------------
>>> name.close()
>>>
//删除后将不能继续写入,除非重新读入
>>> name.write("hello 3")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>>
--------------------------查看name.txt文件变化------------------------------------
[root@localhost ~]# cat name.txt
hello
hello 222
hello 3[root@localhost ~]#
>>> name = file("name.txt","a") //(将name.txt文件读入到内存中,且允许对原文件追加写入 a表示允许追加写入 )
-----------------------------写入新内容到name.txt文件中--------------------------------
>>> name.write("hello 3")
-----------------------------将文件从内存中删除,并且保存到硬盘中-----------------
>>> name.close()
>>>
//删除后将不能继续写入,除非重新读入
>>> name.write("hello 3")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>>
--------------------------查看name.txt文件变化------------------------------------
[root@localhost ~]# cat name.txt
hello
hello 222
hello 3[root@localhost ~]#
相关文章推荐
- python 造数
- python print如何不换行输出
- python 输出正方形
- python 如何加密自己的脚本
- python交互读入
- python程序中在线文档编辑
- Python中几个input方法
- python自动补全
- python mysql
- python pdb调试以及sublime3快捷键设置
- python笔记002——条件和循环
- Python中调用设置环境变量的bat
- python 入门笔记(四)
- python 装饰器
- python初学--day1
- python学习笔记(六)切片
- python学习笔记(七)迭代
- python学习笔记(五)函数
- Python列表添加元素操作
- Windows系统下如何安装Python以及对应pygame