python逐行读写txt文件的实例讲解
2018-04-03 10:30
676 查看
# -*-coding:utf-8-*- import os file_obj = open("test2.txt") all_lines = file_obj.readlines() for line in all_lines: print line file_obj.close() # 写之前,先检验文件是否存在,存在就删掉 if os.path.exists("dest.txt"): os.remove("dest.txt") mylist = ["luoluo", "taotao", "mumu"] # 以写的方式打开文件,如果文件不存在,就会自动创建 file_write_obj = open("dest.txt", 'w') for var in mylist: file_write_obj.writelines(var) file_write_obj.write('\n') file_write_obj.close()
w 以写方式打开,
a 以追加模式打开
r+ 以读写模式打开
w+ 以读写模式打开
a+ 以读写模式打开
以上这篇python逐行读写txt文件的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Python 批量合并多个txt文件的实例讲解
- python 文件读写5个实例
- ObjectARX中读写txt文件的一个类实例
- Python 读写txt文件
- Python 文件读写操作实例详解
- paip.文件读写api php java python总结.txt
- python 简单的txt文件读写
- Python读写txt文本文件的操作方法全解析
- python操作excel文件并输出txt文件的实例
- python读写不同编码txt文件
- Python读写txt文本文件的操作方法全解析
- Python 实现删除某路径下文件及文件夹的实例讲解
- python读写不同编码txt文件
- Python读写txt文本文件的操作方法全解析
- Python 文件读写操作实例详解
- Python小练习1:.txt文件常用读写操作
- python 读写txt文件 json文件
- Python 文件读写操作实例详解
- Python txt文件的读写
- Python读写txt文本文件的操作方法全解析