Python provides inbuilt functions for creating, writing and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language,
1s). In this article, we are going to study about reading line by line from a file.
Reading line by line
readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines. We can iterate over the list and strip the newline
'\n' character using
Line1: Geeks Line2: for Line3: Geeks
readline() function reads a line of the file and return it in the form of the string. It takes a parameter n, which specifies the maximum number of bytes that will be read. However, does not reads more than one line, even if n exceeds the length of the line. It will be efficient when reading a large file because instead of fetching all the data in one go, it fetches line by line.
readline() returns the next line of the file which contains a newline character in the end. Also, if end of the file is reached, it will return an empty string.
Line1 Geeks Line2 for Line3 Geeks
Using for loop
An iterable object is returned by
open() function while opening a file. This final way of reading in a file line-by-line includes iterating over a file object in a
for loop. Doing this we are taking advantage of a built-in Python functionality that allows us to iterate over the file object implicitly using a
for loop in combination of using the iterable object. This approach takes fewer lines of code, which is always the best practice worthy of following.
Using for loop Line1: Geeks Line2: for Line3: Geeks
In the above approaches, every time the file is opened it is needed to be closed explicitly. If one forgets to close the file, it may introduce several bugs in the code, i.e. many changes in files do not go into effect until the file is properly closed. To prevent this
with statement can be used.
with statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Observe the following code example on how the use of with statement makes code cleaner. There is no need to call
file.close() when using with statement. The
with statement itself ensures proper acquisition and release of resources.
Using readlines() Line1: Geeks Line2: for Line3: Geeks Using readline() Line1: Geeks Line2: for Line3: Geeks Using for loop Line1: Geeks Line2: for Line3: Geeks
- How to read from a file in Python
- Read JSON file using Python
- Python program to read character by character from a file
- Python program to read file word by word
- Upload file and read its content in cherrypy python
- Read List of Dictionaries from File in Python
- How to read Dictionary from File in Python?
- How to read a CSV file to a Dataframe with custom delimiter in Pandas?
- reStructuredText | .rst file to HTML file using Python for Documentations
- Create a GUI to convert CSV file into excel file using Python
- Python - Get file id of windows file
- Python program to reverse the content of a file and store it in another file
- Python - Read blob object in python using wand library
- Python program to Reverse a single line of a text file
- MoviePy – Getting Original File Name of Video File Clip
- PYGLET – Opening file using File Location
- Python | Read csv using pandas.read_csv()
- Python | OpenCV program to read and save an Image
- Python | os.read() method
- Read latest news using newsapi | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham singh 29