🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeHow to Handle Files in Python

Working with files in Python starts with deciding whether your program needs to read data or write it. File handling lets Python read from a named file or write data to it, and open() returns the file object used to do that.
I’ll explain how to choose a file mode and use a context manager when working with text files.
TL;DR
Use with open() to read or write a file, because the context manager closes it when the block ends.
- Use r to read, w to replace content, and a to append.
- Specify encoding=”utf-8″ for text files.
- Use x when creating a file must fail if its path already exists.
What Is File Handling in Python?
File handling lets a program read data from a named file or write data to it. Python’s open() returns a file object, which carries the operations and state for that open file.
A path identifies where the file is. A mode tells Python what you intend to do.
How to Handle a Text File Step by Step
The example writes and reads a text file. The terminal output shows what Python read.
Step 1: Choose a file mode
Mode w replaces old content, so I use it only for this disposable example file.
| Mode | Use | When the path is missing |
|---|---|---|
| r | Read | Raises FileNotFoundError |
| w | Write, replacing existing contents | Creates the file |
| a | Add data at the end | Creates the file |
| x | Create without replacing | Creates the file |
Mode r+ permits reading and writing through one handle, but its file position affects each operation. Separate reads and writes are clearer here.
Step 2: Write text with a context manager
Use with to open the file. Python closes it when the block ends.
from pathlib import Path
path = Path("notes.txt")
path.write_text("First line\nSecond line\n", encoding="utf-8")
with path.open("r", encoding="utf-8") as file:
print(file.read(), end="")
Run the saved program with python3 file_handling_demo.py. Its output is:
First line
Second line

The output has one line for each string written to notes.txt. Path.write_text() opens and closes the file internally, while Path.open() gives you a file object to manage in a with block.
Step 3: Read a file line by line
A file object can be iterated directly to yield one line at a time. I use this when a file may be too large to hold in memory.
read() returns the remaining contents as one string. For line-by-line work, iteration avoids building a list of the entire file.
A relative path is resolved from the process’s current working directory, which may differ from the directory containing the script. pathlib.Path represents a path as an object and supports joining path parts with the slash operator. Use it when building a path from a directory and a filename instead of assembling separators by hand.
Text files may use different newline conventions. Python’s text mode translates line endings when reading, so a loop can work with lines without checking the platform-specific separator. The rstrip() call in the example removes the trailing newline before printing.
File Handling Errors and Choices
File errors usually point to the path, mode, or text encoding. I check those inputs first because changing the operation alone will not fix a misspelled path or mismatched encoding.
| Symptom | Check |
|---|---|
| FileNotFoundError | Confirm the path is relative to the program’s current working directory, or use an absolute path. |
| Existing data disappears | Mode w truncates the file when it opens. Choose a to append or x to refuse an existing path. |
| UnicodeDecodeError | Use the encoding that matches the file. UTF-8 is a common choice, but files can use another encoding. |
| Large file uses too much memory | Iterate over the file object rather than calling read() without a size. |
Catch FileNotFoundError where the program can recover from a missing path. Mode x raises FileExistsError for an existing target.
Use read(size) to request a bounded chunk. Calling read() without a size returns all remaining contents, which suits short files but can consume substantial memory for large ones.
Python’s open() documentation lists mode and encoding behavior. For paths and file operations, the site’s Python standard-library overview is a useful next reference. If your next task is organizing code across modules, see how to import other Python files.
Conclusion: Use a Context Manager
Choose the mode to match the action and specify a text encoding. The context manager closes the handle after the operation.
FAQ
These answers clarify two common terms and defaults in Python file I/O.
What is a file handle in Python?
The object returned by open() is usually called a file object. It provides methods such as read() and write() and represents an open file in the program.
What is the default mode for open()?
The default mode is r, which opens an existing file for reading text. Specify another mode when you need to write, append, create, or read bytes.


