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

2023-01-31 05:01:14 文件 读取 三种

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

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

相关文章