如何在python中将整个文件读入列表?

Mir*_*ash 3 python

我想将整个文件读入python列表,任何人都知道如何?

Max*_*Max 6

print "\nReading the entire file into a list."
text_file = open("read_it.txt", "r")
lines = text_file.readlines()
print lines
print len(lines)
for line in lines:
    print line
text_file.close()
Run Code Online (Sandbox Code Playgroud)


nin*_*cko 6

更简单:

with open(path) as f:
    myList = list(f)
Run Code Online (Sandbox Code Playgroud)

如果你不想要换行,你可以这样做 list(f.read().splitlines())