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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5
Reading & Writing Text Files
Swipe to show menu
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.
Thanks for your feedback!