python 文件处理模块的使用,给一个文件写入数据
2015-10-28 02:35
507 查看
-------------------------读入一个不存在的文件到内存,且允许写入数据,赋值给变量name-------------------------------
>>> name = file('name.txt','w') //(读取一个已存在的文件,将会把文件内的内容清空,w为允许写入,默认为r只读)
-------------------------写入数据--------------------------------
name.write("hello \n")
-------------------------刷新该文件--------------------------------
>>> name.flush()
>>>
------------------------查看是否生成name.txt----------------
[root@localhost ~]# cat name.txt
hello
[root@localhost ~]#-------------------------再次写入数据--------------------------------
>>> name.write("hello 222\n")
----------------------从内存中删除该文件,并且保存------------
>>> name.close()
//退出后,再次写入发现无法读取该文件,证明删除成功。
>>> name.write("hello 222\n")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>>
-------------------------查看该文件状态-------------------------
[root@localhost ~]# cat name.txt
hello
hello 222
[root@localhost ~]#
>>> name = file('name.txt','w') //(读取一个已存在的文件,将会把文件内的内容清空,w为允许写入,默认为r只读)
-------------------------写入数据--------------------------------
name.write("hello \n")
-------------------------刷新该文件--------------------------------
>>> name.flush()
>>>
------------------------查看是否生成name.txt----------------
[root@localhost ~]# cat name.txt
hello
[root@localhost ~]#-------------------------再次写入数据--------------------------------
>>> name.write("hello 222\n")
----------------------从内存中删除该文件,并且保存------------
>>> name.close()
//退出后,再次写入发现无法读取该文件,证明删除成功。
>>> name.write("hello 222\n")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>>
-------------------------查看该文件状态-------------------------
[root@localhost ~]# cat name.txt
hello
hello 222
[root@localhost ~]#
相关文章推荐
- python 文件处理模块的使用,如何读取文件中数据
- python 文件处理模块使用,对一个文件追加写入
- 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学习笔记(五)函数