python逐行读写txt文件的实例讲解
实例如下所示:
#-*-coding:utf-8-*-
importos
file_obj=open("test2.txt")
all_lines=file_obj.readlines()
forlineinall_lines:
printline
file_obj.close()
#写之前,先检验文件是否存在,存在就删掉
ifos.path.exists("dest.txt"):
os.remove("dest.txt")
mylist=["luoluo","taotao","mumu"]
#以写的方式打开文件,如果文件不存在,就会自动创建
file_write_obj=open("dest.txt",'w')
forvarinmylist:
file_write_obj.writelines(var)
file_write_obj.write('\n')
file_write_obj.close()
w以写方式打开,
a以追加模式打开
r+以读写模式打开
w+以读写模式打开
a+以读写模式打开
以上这篇python逐行读写txt文件的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短