Javatpoint Logo
Javatpoint Logo

How to read a file line by line in python?

Reading a file line by line in Python is common in many data processing and analysis workflows. Here are the steps you can follow to read a file line by line in Python:

1. Open the file: Opening the desired file is the first step. To do this, you can use the built-in open() function, which takes two arguments: the name of the file you want to open, and the mode in which you want to open it. For example, if you want to open a file named txt in read-only mode, you can use the following code:

2. Read the file line by line: Once you have opened the file, you can read its contents line by line using a loop. One way to do this is to use a for() loop to iterate over the file object. It will automatically read the file line by line, and each line will be stored as a string in the loop variable. Here's an example:

In this example, each file line will be stored in the line variable, and you can perform any operations you want inside the loop.

3. Close the file: If you want to close the file once you have finished reading it, you can use the close() method. It will release any system resources that were being used by the file. Here's an example:

It is a good practice always to close the file after reading it, as it will prevent any potential issues with file handles or system resources.

Example:

Let's take an example that illustrates how to read a file line by line:

In this example, we're printing each line of the file to the console, after removing any leading or trailing whitespace using the strip() method. You can modify the code inside the loop to perform any other operations you want on each line of the file.

Here are a few more examples of how to read a file line by line in Python, using different techniques and methods:

Reading a file using a while loop:

Instead of using a for loop, you can also read a file using a while loop and the readline() method. The readline() method reads a single line from the file and returns it as a string. Here's an example:

In this example, we first open the file using the open() function. After that, we call readline() method to read the first line of the file, and store it in the line variable. After applying the readline() method, we enter a while loop that continues as long as line is not an empty string (which indicates that we have reached the end of the file). Inside the loop, we print the line to the console and call readline() again to read the next line of the file. Finally, we close the file using the close() method.

Using a context manager to automatically close the file:

When you open a file using the open() function, you should always remember to close it using the close() method. However, it's easy to forget this, especially if your code is complex or if you encounter an error. You can use a context manager instead to ensure the file is always closed. A context manager is an object that automatically manages resources (such as files) and releases them when they are no longer needed. Here's an example:

In this example, we use with statement to create a context manager that opens the file and assigns it to the variable file. After that, same as before, we read the file line by line using a "for" loop. However, because we are using a context manager, so we don't need to worry about closing the file manually. The context manager will take care of that for us when the block is exited.

Reading a file into a list:

Use the readlines() method to read a file into a list where each element of the list represents a line of the file. With this method, every line of the file is read and converted into a list of strings, each string representing a line of the file. Here's an example:







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA