Learn Python File Handling step by step! This tutorial covers creating, reading, writing, appending, and managing files using Python. Perfect for beginners, students, and developers who want to master file operations efficiently.
- Introduction
- File Operations Overview
- File Modes
- Reading Files
- Writing Files
- Appending Files
- Using with Statement
- Other Useful Methods
- Examples
- Resources
File Handling in Python allows programs to create, read, write, update, and manage files.
With file handling, you can store data permanently or retrieve it when needed.
Common operations include:
- Open – Access a file
- Read – Read file content
- Write – Write content to a file
- Append – Add content to the end of a file
- Close – Close the file safely
| Operation | Function / Mode | Description |
|---|---|---|
| Open file | open(filename, mode) |
Access a file |
| Read file | file.read() / file.readline() / file.readlines() |
Read file content |
| Write file | file.write() |
Write new content |
| Append file | file.write() with 'a' mode |
Add content to file end |
| Close file | file.close() |
Close file |
| With statement | with open() as file: |
Automatically open and close file |
| Mode | Description |
|---|---|
'r' |
Read only (default) |
'w' |
Write only (overwrite if exists) |
'a' |
Append only (add at end) |
'r+' |
Read and write |
'rb' |
Read binary |
'wb' |
Write binary |
'ab' |
Append binary |
Read a file's content using "r" mode.
with open("example.txt", "r") as file:
content = file.read()
print(content)
python, python-file-handling, file-operations, python-tutorial, python-beginner, python-files, read-write-files