Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — File Handling: Creation, Writing, Appending

Lesson 48 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

File Handling — Creation, Writing, Appending

Creating and Writing a File — mode 'w'

'w' mode creates a new file if it does not exist, and overwrites it completely if it does.

with open("diary.txt", "w") as f:
    f.write("Day 1: Learned Python basics.\n")
    f.write("Day 2: Practiced control structures.\n")

writelines() — write multiple lines from a list

lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open("output.txt", "w") as f:
    f.writelines(lines)

Reading a File

with open("diary.txt", "r") as f:
    content = f.read()          # reads the ENTIRE file as one string
    print(content)

with open("diary.txt", "r") as f:
    for line in f:                # reads line by line (memory efficient)
        print(line.strip())

with open("diary.txt", "r") as f:
    lines = f.readlines()          # returns a LIST of lines
    print(lines)

Appending to a File — mode 'a'

'a' mode adds content to the end of an existing file without erasing prior content. If the file doesn't exist, it is created.

with open("diary.txt", "a") as f:
    f.write("Day 3: Learned about file handling.\n")

with open("diary.txt", "r") as f:
    print(f.read())
# Day 1: Learned Python basics.
# Day 2: Practiced control structures.
# Day 3: Learned about file handling.

'w' vs 'a' — critical distinction

with open("test.txt", "w") as f:
    f.write("First write")

with open("test.txt", "w") as f:   # 'w' OVERWRITES -- "First write" is lost
    f.write("Second write")
print(open("test.txt").read())      # "Second write" only

with open("test.txt", "a") as f:     # 'a' APPENDS -- keeps existing content
    f.write(" + appended text")
print(open("test.txt").read())        # "Second write + appended text"

Writing numbers to a file (must convert to string)

marks = [85, 90, 78]
with open("marks.txt", "w") as f:
    for m in marks:
        f.write(str(m) + "\n")   # write() requires a string, not int