readline() in Python Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The readline() method in Python is used to read a single line from a file. It is helpful when working with large files, as it reads data line by line instead of loading the entire file into memory.Syntaxfile.readline(size)Parameterssize (Optional): The number of bytes from the line to return. Default -1, which means the whole line.Return ValueReturns an empty string ('') when the end of the file is reached.Examples of readline() First, let's create a file called example.txt with the following content:This is the first line.This is the second line.This is the third line.This is the fourth line.1. Reading a Single Line Python with open("example.txt", "r") as file: line = file.readline() print(line) # Prints the first line of the file Output:This is the first line.Explanation:file.readline() reads the first line from the file.It prints the first line, which is "This is the first line.", including the newline character (\n) at the end of the line.2. Reading Multiple Lines with a Loop Python with open("example.txt", "r") as file: while True: line = file.readline() if not line: break # Stop when end of file is reached print(line.strip()) Output:This is the first line.This is the second line.This is the third line.This is the fourth line.Explanation:The while True: loop keeps reading lines until the end of the file is reached. file.readline() reads one line at a time.if not line: checks if the line is empty (which happens when the end of the file is reached). When it finds an empty line, it breaks out of the loop.line.strip() removes any trailing newline characters from the line before printing it.3. Using readline() with a Specific Character Limit Python with open("example.txt", "r") as file: line = file.readline(10) print(line) Output:This is theExplanation:file.readline(10) reads the first 10 characters of the first line in the file. It stops reading after the 10th character, regardless of whether it's at the end of the word or not.The print(line) statement will print exactly those 10 characters.Difference Between readline(), readlines(), and read()MethodDescriptionreadline()Reads one line at a timereadlines()Reads all lines and returns them as a listread()Reads the entire file as a single string Comment P pragatikheuvg Follow Improve P pragatikheuvg Follow Improve Article Tags : Python Python Programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 1 min read Like