python 读写文件,按行修改文件
2016-09-12 18:02
405 查看
>>> f = open(r'E:\python\somefile.txt','w') 打开文件,写模式 >>> f.write('this\nis no \nhailu') 写入三行话 17 >>> f.close() >>> f = open(r'E:\python\somefile.txt','r') >>> f.read() 'this\nis no \nhailu' 查看一下 >>> f = open(r'E:\python\somefile.txt') >>> lines = f.readlines() 把每一行的内容变为集合lines 的一个元素
>>> f.close() >>> lines[1] = "isn't a\n" 给lines的第二个元素 重新赋值(改写了) >>> f = open(r'E:\python\somefile.txt','w') >>> f.writelines(lines) >>> f.close() >>
改写后的文件打开就是这个样子
<pre name="code" class="python">this isn't a hailu
相关文章推荐
- python 读写文件,按行修改文件的方法
- python写文件、读取文件、按行修改文件
- 【Python】修改文件并立即写回到原始位置(inplace读写)
- Python基础知识之文件读写与修改
- python读写文件数据(换行写入以及按行读取)
- python (11)文件的读写 按行读文件
- Python读写,以及修改my.ini文件--针对Python3.0版本
- Python学习笔记--文件读写
- python读写UCS-2 big/little endian(utf-16-be/utf-16-le)格式的文件
- Python读写文件
- Linux系统如何查看及修改文件读写权限查看文件权限的语句:
- Python读写文件
- Python读写文件及文件指针定位
- Python文件的读写
- Python天天美味(17) - open读写文件
- 测试成功的Python中文文件读写脚本
- [Python]读写文件方法
- Python open读写文件实现脚本
- python正确的文件读写
- python Ini文件读写