Python Tutorial in Hindi #16 - File Handling | Write, Append & Delete Files
Python File Handling – Writing & Deleting Files
In this video, Mohit Decodes walks you through how to write, append, and delete files using Python. You’ll explore the file modes "w", "a", and "w+", and understand how data is written and updated in files. We’ll also cover file deletion using the os module. These concepts are essential for working with logs, reports, user data, or config files in real-world applications.
✅ Topics Covered:
Writing to Files using "w" Mode
Appending Data using "a" Mode
Read & Write with "w+" and "a+"
Deleting Files using os.remove()
Real-life Examples: Logging & Configs
Interview Questions on File Handling
# ================================
# Python File Handling - Write, Append & Delete Files
# ================================
# Author: Mohit Decodes
# ------------------------------
# 🧩 Example 1: Writing to a File (write mode - 'w')
# ------------------------------
# If the file already exists, this mode will overwrite it.
# If it doesn't exist, it will create a new one.
file = open("demo.txt", "w")
file.write("Hello World!\n")
file.write("Welcome to Mohit Decodes\n")
file.write("Learning File Handling in Python.\n")
file.close()
print("✅ Data written successfully in demo.txt")
# ------------------------------
# 🧩 Example 2: Appending Data (append mode - 'a')
# ------------------------------
# This mode adds new data to the end of the file without deleting existing content.
file = open("demo.txt", "a")
file.write("Appending new line to the file.\n")
file.write("Python makes file handling easy!\n")
file.close()
print("✅ Data appended successfully to demo.txt")
# ------------------------------
# 🧩 Example 3: Reading After Writing (read mode - 'r')
# ------------------------------
# Let's check the content of demo.txt
file = open("demo.txt", "r")
print("----- File Content -----")
print(file.read())
file.close()
# ------------------------------
# 🧩 Example 4: Write and Read Together (mode - 'w+')
# ------------------------------
# 'w+' allows both write and read.
# However, it overwrites the existing content.
file = open("demo2.txt", "w+")
file.write("This is written using w+ mode.\n")
file.write("It can also read data after writing.\n")
# Move cursor back to the beginning using seek(0)
file.seek(0)
print("----- demo2.txt Content -----")
print(file.read())
file.close()
# ------------------------------
# 🧩 Example 5: Append and Read Together (mode - 'a+')
# ------------------------------
# 'a+' allows reading and appending (won’t overwrite existing data)
file = open("demo2.txt", "a+")
file.write("This line is added using a+ mode.\n")
# Move cursor to beginning to read
file.seek(0)
print("----- Updated demo2.txt Content -----")
print(file.read())
file.close()
# ------------------------------
# 🧩 Example 6: Writing Using with open() (Auto Close)
# ------------------------------
with open("notes.txt", "w") as f:
f.write("This is a new file created using 'with open'.\n")
f.write("This method automatically closes the file.\n")
print("✅ notes.txt created successfully!")
# ------------------------------
# 🧩 Example 7: Deleting a File
# ------------------------------
import os
# Check if file exists before deleting
if os.path.exists("demo2.txt"):
os.remove("demo2.txt")
print("🗑️ demo2.txt deleted successfully!")
else:
print("❌ demo2.txt does not exist!")
# ------------------------------
# 🧩 Example 8: Handling FileNotFoundError
# ------------------------------
try:
os.remove("unknown.txt")
except FileNotFoundError:
print("⚠️ File not found! Nothing to delete.")
# ------------------------------
# 🧩 Example 9: Practice Task
# ------------------------------
# ✅ Create a new file ‘log.txt’
# ✅ Append data multiple times (like logging info)
# ✅ Read and display all lines
with open("log.txt", "a+") as log:
log.write("Log Entry 1: Program started.\n")
log.write("Log Entry 2: File handling executed.\n")
log.write("Log Entry 3: Program ended successfully.\n")
log.seek(0)
print("----- Log File Content -----")
print(log.read())