Home >>Python File Methods >Python File readline() Method

Python File readline() Method

Python File readline() Method

The readline() method in python file is used to read one line from the file. By using the size parameter, you can also return the bytes from the line and kept a trailing newline character in the string.

Syntax:
file.readline(size)

Parameter Values

Parameter Description
size It is Optional. The number of bytes from the line to be read.
Here is an example of Python File readline() Method:

x = open("file1.txt", "r")
print(x.readline())

Output:
Hello! Welcome to file1.txt
Example 2:

y = open("file2.txt", "r")
print(y.readline())

Output:
Hello! Welcome to file2.txt