ITPub博客

首页 > IT职业 > IT生活 > Python之读取TXT文件的三种方法

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

IT生活 作者:tomorrowcome 时间:2021-09-09 09:35:04 0 删除 编辑


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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/875/viewspace-2804930/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: Headless Chrome 入门
请登录后发表评论 登录
全部评论

注册时间:2019-08-19

  • 博文量
    122
  • 访问量
    928