Python Read and Write Text File Example
This example shows a small Python script that writes text to a file and then reads it back.
It is a practical beginner example. The goal is simple: create or overwrite a text file, add some text, read the file, and print the result.
Quick example
file_name = "notes.txt"
with open(file_name, "w") as file:
file.write("Hello, Python!\n")
file.write("This is a text file example.\n")
with open(file_name, "r") as file:
content = file.read()
print(content)
This creates or overwrites a text file, writes two lines, reads the full file, and prints the result.
What this example shows
- How to open a file for writing with mode
"w" - How to write text using
write() - How to open the same file for reading with mode
"r" - How to read all file content with
read() - Why using
with open(...)is the beginner-friendly safe pattern
If you want a fuller introduction, see Python file handling basics: read and write or the Python open() function explained.
Example setup
In this example, the file name is:
file_name = "notes.txt"
This means Python will look for the file in the current folder where your script runs.
A few important points:
"w"means write mode- If the file does not exist, Python creates it
- If the file already exists,
"w"removes the old content first \nadds a new line
So this line:
file.write("Hello, Python!\n")
writes the text and then moves to the next line.
Write text to the file
Here is the writing part by itself:
file_name = "notes.txt"
with open(file_name, "w") as file:
file.write("Hello, Python!\n")
file.write("This is a text file example.\n")
What to notice:
open(file_name, "w")opens the file in write modewithautomatically closes the file when the block endswrite()writes exactly the string you give itwrite()does not add a new line by itself
If you want to learn more about writing files, see how to write to a file in Python.
Read text from the file
Here is the reading part:
with open(file_name, "r") as file:
content = file.read()
print(content)
What this does:
open(file_name, "r")opens the file in read modefile.read()reads the whole file as one string- The result is stored in
content print(content)shows the text in the terminal
If you want more reading examples, see how to read a file in Python.
Expected output
The script prints:
Hello, Python!
This is a text file example.
This matches the two lines written earlier.
That is the main idea of this example:
- Write text into the file
- Read the same text back
- Print it so you can see the result
How this example works step by step
1. The first block opens the file and writes text
with open(file_name, "w") as file:
file.write("Hello, Python!\n")
file.write("This is a text file example.\n")
This creates notes.txt if needed and writes two lines into it.
2. The file is closed automatically
When the with block ends, Python closes the file for you.
This is why with open(...) is the safest beginner pattern.
3. The second block opens the file again for reading
with open(file_name, "r") as file:
content = file.read()
Now Python opens the same file in read mode.
4. read() returns the file content as a string
The full contents of the file are stored in content.
5. print() displays that string
print(content)
This prints the file text to the terminal or console.
Common beginner mistakes
Here are some common problems with this kind of script:
- Using
"w"when you wanted to keep old file content - Forgetting
\nand expecting separate lines - Trying to read before the file was written
- Using the wrong file path
- Misspelling the file name
Also watch out for these common causes:
- Using write mode
"w"and accidentally overwriting existing content - Reading a file from a different folder than expected
- Forgetting newline characters when writing multiple lines
- Confusing text file reading with CSV or JSON reading
- Trying to open a file that does not exist when using
"r"
If Python says the file cannot be found, see how to fix FileNotFoundError: [Errno 2] No such file or directory.
Useful debugging checks:
print(file_name)
import os
print(os.getcwd())
import os
print(os.path.exists(file_name))
with open(file_name, 'r') as file:
print(repr(file.read()))
These help you check:
- the file name you are using
- the current working folder
- whether the file actually exists
- the exact text in the file, including
\n
Try a small variation
Once this example works, try changing one small part at a time:
- Change the file name
- Write three lines instead of two
- Replace
read()withreadline()orreadlines() - Switch from
"w"to"a"and see what changes
For example, append mode keeps the old content instead of replacing it:
file_name = "notes.txt"
with open(file_name, "a") as file:
file.write("This line was added later.\n")
If you want to practice that next, see how to append to a file in Python.
FAQ
What does "w" mean in open()?
"w" means write mode. It creates a new file if needed and overwrites old content if the file already exists.
What does "r" mean in open()?
"r" means read mode. It opens an existing file so you can read its contents.
Why use with open() instead of open() by itself?
with open() closes the file automatically, which is safer and easier for beginners.
How do I keep existing text instead of replacing it?
Use append mode "a" instead of write mode "w".
Why is my text all on one line?
write() does not add line breaks automatically. Add \n where you want a new line.