python 同时读取多个文件的例子
Python中打开文本使用的是with语句,比如打开一个文件并读取每一行
withopen(filename)asfp: forlineinfp: #dosomething
为了同时读取多个文件,可以使用下面的代码
withopen(filename1)asfp1,open(filename2)asfp2,open(filename3)asfp3: forl1infp1: l2=fp2.readline() l3=fp3.readline() #dosomething
稍微简介一点可以使用contextlib中的nested,有
fromcontextlibimportnested withnested(open(filename1),open(filename2),open(filename3))as(fp1,fp2,fp3): forl1infp1: l2=fp2.readline() l3=fp3.readline() #dosomething
以上这篇python同时读取多个文件的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 生日贺卡祝福语简短创意
11 公司最好的祝福语简短
12 答谢生子祝福语简短独特
13 升职加薪的祝福语简短
14 新年简短祝福语给亲人
15 白族结婚祝福语简短大全
16 新年酒会祝福语简短创意
17 财神祝福语简短网名
18 给妈妈祝福语简短内容