Here you will learn alternative ways of getting file data and convert it to a list.
Python has a built-in open() function that returns a file-like object which acts as an iterator.
We can also use the fdopen()
method from the os
module to read a file. Then there is also the fileinput
module that can be used for the purpose.
Using File object
Use the built-in open() function to read from file stream three methods - read()
, readline()
and readlines()
.
For demonstration purpose, we have a zen.txt
file that has the first few lines from the Zen of Python
:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense.
The following snippet reads this file into a list. The readline()
method contains a newline character at the end, which is removed by the strip()
method of the string object.
fileobj=open("zen.txt")
lines=[]
for line in fileobj:
lines.append(line.strip())
print(lines)
['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']
The readlines()
method readily returns a list without having to perform iteration.
fileobj=open("zen.txt")
lines=fileobj.readlines()
lines=[line.strip() for line in lines]
print(lines)
['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']
We can also use the read()
method and split the string at each newline character with the split()
method.
fileobj=open("zen.txt")
lines=fileobj.read().split('\n')
print(lines)
['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']
Continuing with the File object, we can obtain an iterator from it and construct a list while traversing it.
fileobj=open("zen.txt")
it=iter(fileobj)
lines=[]
while True:
try:
line=next(it)
line=line.strip()
lines.append(line)
except StopIteration:
break
print(lines)
['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']
Convert to List using OS Module
Most of the functionality of the os
module is dependent on operating system, it also has number of tools for file handling.
Here, we use the fdopen()
function, which essentially wraps a file descriptor
in a file object. The file descriptor is obtained by the os.open()
function as follows:
import os
fd=os.open("zen.txt", os.O_RDONLY)
fileobj=os.fdopen(fd)
lines=fileobj.readlines()
lines=[line.strip() for line in lines]
print(lines)
['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']
Convert to List using fileinput Module
Although the real power of this module is it helps in iterating over multiple files provided as command-line argument, we shall use its input()
function to open our zen.txt and read it in a list.
import fileinput
lines=[]
for line in fileinput.input(files=['zen.txt']):
lines.append(line.strip())
print(lines)
['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']
As we can see, there are a lot of options to choose from, when it comes to reading a file line-by-line in a list.