欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python逐行读写txt文件的实例讲解

时间:2021-02-01 09:55:07|栏目:Python代码|点击:

实例如下所示:

# -*-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实现全排列的打印

栏    目:Python代码

下一篇:对PyQt5中树结构的实现方法详解

本文标题:python逐行读写txt文件的实例讲解

本文地址:http://www.codeinn.net/misctech/54806.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有