Reading & Writing Text Files
Working with text files is a common task in programming — for saving data, reading configs, or processing logs.
Text files store content as plain lines of text, separated by newline characters.
In Python, you use the built-in "open"
function to read and write .txt
files.
- Show how to read all lines from a notes.txt file using a "with" block.
- Show how to write multiple lines of text to a file in write mode.
- Show how to open a shopping_list.txt file from a shopping directory using a relative path.
Opening a File
To work with a file, first open it using Python's "open"
function with two arguments: the file name and the mode.
Modes:
"r"
→ read;"w"
→ write (overwrites existing content);"a"
→ append to the end;"x"
→ create new file (error if it already exists).
Reading a File
After opening a file, you can read its contents in different ways:
"read()"
→ entire file as one string;"readline()"
→ a single line;"readlines()"
→ a list of all lines.
Always close the file when finished, or use a "with"
block to handle it automatically.
Writing to a File
To save data, open the file in write or append mode.
- Write mode (
"w"
) clears old content; - Append mode (
"a"
) adds new text at the end.
Use "write()"
to add text, and include "\n"
if you need line breaks.
File Paths
If your file is not in the same folder as your script, you'll need to use a full or relative file path.
Python handles both forward slashes /
and double backslashes \\
depending on your operating system.
Summary
- Use the
"open"
function with different modes to work with text files; - Use
"read()"
,"readline()"
, or"readlines()"
to read file content; - Use
"write()"
or"append()"
to save new content; - Always close the file or use a
"with"
block; - Use file paths when working with files outside your project folder.
Try It Yourself
- Open a file called
"notes.txt"
in write mode; - Write three lines of text into it;
- Reopen the file in read mode;
- Print its contents line by line.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you show me an example code for these steps?
What happens if "notes.txt" doesn't exist when I try to read it?
How do I handle errors when working with files in Python?
Awesome!
Completion rate improved to 5
Reading & Writing Text Files
Veeg om het menu te tonen
Working with text files is a common task in programming — for saving data, reading configs, or processing logs.
Text files store content as plain lines of text, separated by newline characters.
In Python, you use the built-in "open"
function to read and write .txt
files.
- Show how to read all lines from a notes.txt file using a "with" block.
- Show how to write multiple lines of text to a file in write mode.
- Show how to open a shopping_list.txt file from a shopping directory using a relative path.
Opening a File
To work with a file, first open it using Python's "open"
function with two arguments: the file name and the mode.
Modes:
"r"
→ read;"w"
→ write (overwrites existing content);"a"
→ append to the end;"x"
→ create new file (error if it already exists).
Reading a File
After opening a file, you can read its contents in different ways:
"read()"
→ entire file as one string;"readline()"
→ a single line;"readlines()"
→ a list of all lines.
Always close the file when finished, or use a "with"
block to handle it automatically.
Writing to a File
To save data, open the file in write or append mode.
- Write mode (
"w"
) clears old content; - Append mode (
"a"
) adds new text at the end.
Use "write()"
to add text, and include "\n"
if you need line breaks.
File Paths
If your file is not in the same folder as your script, you'll need to use a full or relative file path.
Python handles both forward slashes /
and double backslashes \\
depending on your operating system.
Summary
- Use the
"open"
function with different modes to work with text files; - Use
"read()"
,"readline()"
, or"readlines()"
to read file content; - Use
"write()"
or"append()"
to save new content; - Always close the file or use a
"with"
block; - Use file paths when working with files outside your project folder.
Try It Yourself
- Open a file called
"notes.txt"
in write mode; - Write three lines of text into it;
- Reopen the file in read mode;
- Print its contents line by line.
Bedankt voor je feedback!