How to Read a File in Python
Learn the simplest ways to open and read a file in Python. This page focuses on reading text files safely, understanding what each method returns, and avoiding common beginner mistakes.
Quick answer #
with open("example.txt", "r") as file:
content = file.read()
print(content)
Use with open(...) so Python closes the file automatically after reading.
What this page helps you do #
- Open a text file
- Read the whole file at once
- Read one line at a time
- Choose the right reading method
- Avoid common file-reading errors
The simplest way to read a file #
The most common way to read a text file in Python is:
with open("example.txt", "r") as file:
content = file.read()
print(content)
How this works #
open("example.txt", "r")opens the file in read modewith ... as file:makes sure the file is closed automaticallyfile.read()reads the entire file and returns it as one string
When to use this #
This is a good choice for small text files when you want all content at once.
For a deeper explanation of open(), see Python open() function explained.
Understand the main reading methods #
Python gives you a few main ways to read from a file.
file.read() #
read() returns the entire file as one string.
with open("example.txt", "r") as file:
content = file.read()
print(content)
print(type(content))
Possible output:
Hello
World
<class 'str'>
Use this when:
- The file is small
- You want the complete contents at once
file.readline() #
readline() returns one line from the file each time you call it.
with open("example.txt", "r") as file:
first_line = file.readline()
second_line = file.readline()
print(first_line)
print(second_line)
If the file contains:
Apple
Banana
Cherry
Then the variables will contain:
first_line→"Apple\n"second_line→"Banana\n"
Notice the \n at the end. That is the newline character.
file.readlines() #
readlines() returns a list of all lines.
with open("example.txt", "r") as file:
lines = file.readlines()
print(lines)
Possible output:
['Apple\n', 'Banana\n', 'Cherry\n']
Use this only if you specifically need a list of lines.
Looping over the file #
Looping over the file is often the best choice for larger files.
with open("example.txt", "r") as file:
for line in file:
print(line.strip())
This reads one line at a time instead of loading the whole file into memory.
How to read a file line by line #
A for loop is usually the simplest way to read a file line by line.
with open("example.txt", "r") as file:
for line in file:
print(line)
Why this is useful #
- It works well for larger files
- It is easy to read
- It avoids loading the whole file at once
About the newline character #
Each line usually ends with \n. That means print(line) may leave extra blank lines in the output.
To remove the newline, use strip():
with open("example.txt", "r") as file:
for line in file:
print(line.strip())
If you want to understand strip() better, see Python string strip() method.
For a full line-by-line guide, see how to read a file line by line in Python.
How file paths affect reading #
The path you give to open() matters.
Relative path #
A relative path depends on your current working folder.
with open("example.txt", "r") as file:
print(file.read())
This works only if example.txt is in the current working directory.
Absolute path #
An absolute path gives the full location of the file.
with open("/home/user/example.txt", "r") as file:
print(file.read())
On Windows, it may look like this:
with open(r"C:\Users\YourName\example.txt", "r") as file:
print(file.read())
If the path is wrong #
If the file name, folder, or extension is wrong, Python raises FileNotFoundError.
To learn more, see working with file paths in Python and how to check if a file exists in Python.
What the read mode means #
When you write:
open("example.txt", "r")
the "r" means:
- Open the file for reading
- Read it as text
- The file must already exist
This page focuses on text files. If you are learning the basics of file handling more broadly, see Python file handling basics.
Common beginner mistakes #
Here are some common problems when reading files:
- Using the wrong file path
- Trying to read a file that does not exist
- Forgetting that
read()returns a string - Reading once and expecting the cursor to reset automatically
- Using
readlines()when a simple loop would be easier
Example: read() returns a string #
with open("example.txt", "r") as file:
content = file.read()
print(content.upper())
This works because content is a string.
Example: the file position does not reset automatically #
with open("example.txt", "r") as file:
first = file.read()
second = file.read()
print("First read:", first)
print("Second read:", second)
The second read() usually returns an empty string because the file cursor is already at the end.
Basic debugging steps #
If file reading does not work, try these checks:
- Print the file path you are trying to open
- Check your current working directory
- Confirm the file really exists
- Read a small test file first
- Watch the exact error message
Useful commands:
print(open("example.txt", "r").read())
import os
print(os.getcwd())
import os
print(os.path.exists("example.txt"))
Common causes include:
- Misspelled file name
- Wrong folder or relative path
- Trying to open a missing file
- Confusing text files with CSV or JSON-specific tasks
- Using the file after it has already been closed
If Python says the file does not exist, read FileNotFoundError: No such file or directory fix.
FAQ #
What is the easiest way to read a file in Python? #
Use with open("file.txt", "r") as file: and then call file.read().
How do I read a file one line at a time? #
Open the file with with open(...) as file: and loop with for line in file:.
Why does Python say FileNotFoundError? #
Python cannot find the file at the path you gave. Check the file name, folder, and current working directory.
Should I use read() or readlines()? #
Use read() for the whole file as one string. Use a loop for line-by-line reading. Use readlines() only when you specifically need a list of all lines.