Python File readline() Method
Example
Read the first line of the file "demofile.txt":
f = open("demofile.txt", "r") print(f.readline())
Definition and Usage
The readline() method returns a line from the file.
You can also use the size parameter to specify how many bytes to return from the line.
Syntax
file.readline(size)
Parameter Value
Parameter | Description |
---|---|
size | Optional. The number of bytes returned from the line. The default value is -1, indicating the entire line. |
More Examples
Instance 1
Call readline() twice to return the first and second lines:
f = open("demofile.txt", "r") print(f.readline()) print(f.readline())
Instance 2
Only return the first five bytes of the first line:
f = open("demofile.txt", "r") print(f.readline(5))