π File Handling in Python#
Files help programs store and read data permanently, even after they close. Python makes it super easy to create, read, write, and manage files, so you can save user data, logs, or any important information. Letβs master file handling step-by-step! π
Basics of File Handling ποΈ#
Python works with files using built-in functions and methods, mainly through the open()
function.
1. Opening a File π#
Syntax:
file = open("filename.txt", "mode")
Common modes:
Mode | Meaning |
---|---|
"r" | Read (file must exist) |
"w" | Write (creates or overwrites file) |
"a" | Append (add to the end) |
"r+" | Read & write |
2. Reading from a File π#
- Read whole content:
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()
- Read line by line:
file = open("example.txt", "r")
for line in file:
print(line, end="")
file.close()
3. Writing to a File βοΈ#
- Write to a file (overwrites if exists):
file = open("example.txt", "w")
file.write("Hello, world!\n")
file.write("This is a new file.")
file.close()
- Append to a file (adds at end):
file = open("example.txt", "a")
file.write("\nAppending new line.")
file.close()
4. Using with
Statement (Best Practice) π#
with
automatically closes the file even if errors occur:
with open("example.txt", "r") as file:
content = file.read()
print(content)
Practice Questions with Solutions π#
- Write code to create a file
notes.txt
and write your favorite quote in it. - Write code to read and print all lines from
notes.txt
. - Append your name to
notes.txt
. - Read a file line by line and count the number of lines.
Step-by-Step Mini Project: Todo List Manager ποΈ#
Create a simple program to add and display tasks saved in a tasks.txt
file.
def add_task(task):
with open("tasks.txt", "a") as file:
file.write(task + "\n")
def show_tasks():
print("Your Tasks:")
with open("tasks.txt", "r") as file:
for line in file:
print("-", line.strip())
# Usage
add_task("Complete Python course")
add_task("Go for a walk")
show_tasks()
Checklist for This Chapter β #
- Opened files in different modes (
r
,w
,a
) - Read whole file or line by line
- Wrote and appended data to files
- Used
with
statement for safe file handling
Youβre now ready to store and manage data on your computer through Python! Practice by creating notes, logs, and simple data storage apps! π