将整个文件作为列表读取的首选方法

时间:2013-03-27 19:14:30

标签: python file-io

我刚刚看到DSM向我的answer发送评论create a series of tuples using a for loop并强迫我想知道是否有任何理由使用fileObj.readlines()而不是将fileObj传递给列表。就我所见,两者都给出了相同的结果。唯一的区别是可读性,但考虑到两者具有相同的可读性,应该采用哪种方式?

考虑两个场景

#This will create a tuple of file lines
with open("yourfile") as fin:
    tup = list(fin)

#This is a straight forward way to create a list of file lines
with open("yourfile") as fin:
    tup = fin.readlines()

我试图计时,但它没有多大意义,因为它们都具有相似的性能。

0 个答案:

没有答案