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)
更简单:
with open(path) as f:
myList = list(f)
Run Code Online (Sandbox Code Playgroud)
如果你不想要换行,你可以这样做 list(f.read().splitlines())