How to read an entire line from a text file using Python?

There are various ways to read files in Python. The most common methods for reading files line by line in Python will be covered.

Using this method, a file will be opened and its contents will be divided into separate lines. A list of every line in the file is also returned by this method. To effectively read a whole file, we can use the readlines() function.

Following is an example to remove swap files using file.endswith() method −

Example

Following is an example to read a text file line by line using readlines() method −

# opening the data file
file = open("TutorialsPoint.txt")

# reading the file as a list line by line

# closing the file
file.close()
print(content)


Output

Following is an output of the above code.

['Welcome to Tutorials Point\n', 'This is a new file.\n', 'Reading a file line by line using Python\n', 'Thank You!']


The first line of the text file can be obtained using the readline() method. When we use the readline() method to read the file, as compared to readlines(), only one line will be printed.

Example

Following is an example to read a single line of a text file using readline() method −

file = open("TutorialsPoint.txt")
# getting the starting line of the file
print(start_line)
file.close()


Output

Only one line of text is returned by the readline() function. If you want to read all the lines at once, use readline().

Following is an output of the above code −

Welcome to Tutorials Point


Note − The readline() method, in contrast to its equivalent, only extracts one line from a file. A trailing newline character will also be added to the end of the string by the realine() method.

We may also define a length for the returned line using the readline() method. Without a size, the entire line will be read.

Using While loop

You can use a while loop to read the specified file's content line by line. Open the file in read mode using the open() function first to accomplish that. Use the file handler that open() returned inside a while loop to read lines.

The while-loop uses the Python readline() method to read the lines. When a for-loop is used, the loop ends when the file's end is reached. With a while-loop, however, this is not the case, and you must continually check to see if the file has finished reading. You can therefore use the break statement to end the while -loop whenever the readline() method returns an empty string.

Example

Following is an example to read a text file line by line using while loop −

file = open("TutorialsPoint.txt", "r")
while file:
print(line)
if line == "":
break
file.close()


Output

Following is an output of the above code −

Welcome to Tutorials Point
This is a new file.
Reading a file line by line using Python
Thank You!


Using for loop

Start by opening the file in read-only mode with the Python open() function. A file handler will be returned by the open() function. In your for-loop, use the file handler to read each line from the provided file one at a time. When finished, use the close() function to close the file handler.

Example

Following is an example to read a text file line by line using for loop −

file = open("TutorialsPoint.txt", "r")
for line in file:
print(line)
file.close()


Output

Following is an output of the above code −

Welcome to Tutorials Point
This is a new file.
Reading a file line by line using Python
Thank You!


Using Context Manager

Any programming language requires a delicate approach to file management. Care must be used when handling files to avoid corruption. It is important to remember to close the resource after opening a file. Additionally, Python has a restriction on how many files can be opened at once. Python gives us the Context Manager to help us avoid these issues.

File handling will be secure if Python uses with statements.

• To safely access resource files, use the with statement.
• When Python comes across the with block, it creates a new context.
• Python automatically closes the file resource after the block has finished running.
• The scope of the context is similar to the with statement.

Example

Following is an example to read a text file line by line using with the statement −

# opening the file
with open("TutorialsPoint.txt",'r') as text:

# reading the file by using a for loop
for line in text:

# stripping the newline character from the line
print(line.strip())


Output

This time, the file's lines are read using a for loop. When we use the Context Manager, the file closes on its own whenever its handler exits the scope. The with statement guarantees that the resource is handled properly when the function has done working with the file.

Following is an output of the above code

Welcome to Tutorials Point
This is a new file.
Reading a file line by line using Python
Thank You!