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

Python之读取TXT文件的三种方法

2018-05-04 10:38 1021 查看

参考了https://blog.csdn.net/shandong_chu/article/details/70173952

-- coding: UTF-8 --

import sys

方法一:
#read txt method one
f = open("./image/abc.txt")
line = f.readline()
while line:
print line
line = f.readline()
f.close()

方法二:
#read txt method two
f = open("./image/abc.txt")
for line2 in open("./image/abc.txt"):
print line2

方法三:
#read txt method three
f2 = open("./image/abc.txt","r")
lines = f2.readlines()
for line3 in lines:
print line3

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息