This tutorial will examine several methods and best practices for reading a file line-by-line in the Python programming language.
Reading a file line-by-line will be useful for parsing log files, CSV spreadsheet files, or even reading files you have generated from an application yourself.
Line by Line File Reading – Examples
The examples on this page will read from a text file named mytext.txt with the following content:
Linux Is Very Cool
Using the readlines() Function
The following python code will call the built-in open() function, which creates a file object which can then be read using the readlines() function, which places all of the lines from the given file into a list of string variables:
textFile = open('mytext.txt', 'r') # The first parameter is the path to the file, the second 'r' parameter means the file is opened for reading lines = textFile.readlines() for line in lines: print(line.strip()) # The strip() method will remove newline characters textFile.close() # Close the file safely
Which will output:
Linux Is Very Cool
The close() method is called on the opened file to make sure that Python releases the file – it’s important that this is done whenever reading or writing files to release any locks on the file and make sure all disk operations have completed before the application quits – otherwise data corruption could occur.
Using The readline() Function
Similarly, you can also use readline() to read a single line as a single string. Each time you call readline(), the next line will be read:
textFile = open('mytext.txt', 'r') # This is an infinite loop, created intentionally, which we will break out of when we reach the end of the file while True: line = textFile.readline() # Next line from file # readline() returns an empty variable when the end of the file is reached, which will evaluate as false when used in a boolean test if not line: break # Break out of the infinite loop # If the line is present, print it print(line.strip()) textFile.close()
Which will again output:
Linux Is Very Cool
Best Practice – using with and a for loop
The best method for reading each line from a file is using the with statement on the open() function. The with statement will ensure that resources are properly released when it is finished – so there is no need to close the file when you are done – so all operations are performed safely.
As mentioned at the beginning of this article, the open() function returns a file object. The file object in Python is iterable– meaning that it can be looped over – so we can simply use a for statement to access each line in succession!
with open("mytext.txt") as textFile: for line in textFile: print(line.strip())
More Things to Do
Now that you know how to read the lines from a file into a list, you can do other things with that list – like: