您的位置:首页 > 编程语言 > Python开发

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 文件