PYTHON PROGRAM: FILE HANDLING (ALL
METHODS ILLUSTRATED)
This document contains a complete Python program that demonstrates all important file handling
operations including file creation, reading, writing, appending, cursor control, and deletion using
methods like read(), write(), writelines(), seek(), tell(), and close(). It also shows how to use different
file modes (r, w, a, r+, w+, a+).
# ===========================================
# PYTHON PROGRAM: FILE HANDLING METHODS
# ===========================================
# Modes:
# 'r' -> Read (file must exist)
# 'w' -> Write (creates/overwrites)
# 'a' -> Append (adds to existing file)
# 'r+' -> Read + Write (must exist)
# 'w+' -> Write + Read (creates/overwrites)
# 'a+' -> Append + Read (creates if not exists)
filename = "[Link]"
print("=== FILE CREATION AND WRITING ===")
# Create and write to a new file
with open(filename, "w") as file:
[Link]("Hello, this is the first line.\n")
[Link]("This is the second line.\n")
[Link](["Third line.\n", "Fourth line.\n"])
print("File written successfully in write mode (w).")
print("\n=== FILE READING (read, readline, readlines) ===")
# Read full content
with open(filename, "r") as file:
content = [Link]()
print("\nFull content using read():\n", content)
# Read line by line
with open(filename, "r") as file:
first_line = [Link]()
print("First line using readline():", first_line.strip())
remaining_lines = [Link]()
print("Remaining lines using readlines():", remaining_lines)
print("\n=== FILE APPENDING (a) ===")
with open(filename, "a") as file:
[Link]("Fifth line (appended).\n")
[Link]("Sixth line (appended).\n")
print("New lines appended successfully.")
print("\n=== FILE READING AFTER APPEND ===")
with open(filename, "r") as file:
print([Link]())
print("\n=== USING 'r+' MODE (Read + Write) ===")
with open(filename, "r+") as file:
data = [Link]()
print("Data before writing:\n", data)
[Link](0) # move cursor to start
[Link]("Updated first line!\n")
print("Cursor position after writing:", [Link]())
print("File updated using r+ mode.")
print("\n=== USING 'a+' MODE (Append + Read) ===")
with open(filename, "a+") as file:
[Link]("Seventh line (written in a+ mode).\n")
[Link](0)
print("Reading full content after a+ operation:\n", [Link]())
print("\n=== FILE POINTER OPERATIONS (seek, tell) ===")
with open(filename, "r") as file:
print("Initial position:", [Link]())
[Link](10)
print("After seek(10): position =", [Link]())
print("Reading from position 10 onwards:", [Link](20))
print("\n=== FILE EXISTS CHECK AND DELETION ===")
import os
if [Link](filename):
print(f"File '{filename}' exists.")
else:
print(f"File '{filename}' does not exist.")
# Uncomment to delete file (optional)
# [Link](filename)
# print(f"File '{filename}' deleted.")
print("\n=== USING 'with' STATEMENT ===")
# 'with' automatically closes the file
with open(filename, "r") as file:
print("File content (using with):")
print([Link]())
print("File automatically closed after with block.")
print("\n=== FILE CLOSED CHECK ===")
file = open(filename, "r")
print("File opened manually:", not [Link])
[Link]()
print("File closed manually:", [Link])
print("\n--- SUMMARY OF FILE METHODS ---")
print("""
open() -> Opens a file
read() -> Reads full content
readline() -> Reads one line
readlines() -> Reads all lines into list
write() -> Writes string
writelines() -> Writes list of strings
seek() -> Moves cursor
tell() -> Shows cursor position
close() -> Closes file
[Link]() -> Deletes file
with open() -> Context manager (auto close)
""")
print("--- END OF PROGRAM ---")