Filehandling

Mon 30 June 2025
# Writing a file
file = open("sara.txt", "w")
file.write("Sara\n")
file.close()
# Reading the file
file = open("sara.txt", "r")
print(file.read())
file.close()
Sara
# Writing multiple lines
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
file = open("lines.txt", "w")
file.writelines(lines)
file.close()
# Reading multiple lines
file = open("lines.txt", "r")
print(file.readlines())
file.close()
['Line 1\n', 'Line 2\n', 'Line 3\n']
# Appending to a file
file = open("sara.txt", "a")
file.write("Keep shining!\n")
file.close()
# Read after append
with open("sara.txt", "r") as f:
    print(f.read())
Sara
Keep shining!
# Reading line by line
with open("lines.txt", "r") as f:
    for line in f:
        print("Line:", line.strip())
Line: Line 1
Line: Line 2
Line: Line 3
# Using with to write
with open("safe.txt", "w") as f:
    f.write("This is safe.")
# File modes demo
modes = ['r', 'w', 'a', 'r+']
print("Modes:", modes)
Modes: ['r', 'w', 'a', 'r+']
# Try reading a non-existent file
try:
    open("nofile.txt", "r")
except FileNotFoundError as e:
    print("Error:", e)
Error: [Errno 2] No such file or directory: 'nofile.txt'
# 'r+' mode
with open("lines.txt", "r+") as f:
    print("Reading:", f.read())
Reading: Line 1
Line 2
Line 3
# 'a+' mode
with open("lines.txt", "a+") as f:
    f.write("Appended via a+\n")
    f.seek(0)
    print(f.read())
Line 1
Line 2
Line 3
Appended via a+
# Checking if file is closed
f = open("temp.txt", "w")
f.write("Testing closed")
print("Closed?", f.closed)
f.close()
print("Closed after closing?", f.closed)
Closed? False
Closed after closing? True
# File pointer position
with open("lines.txt", "r") as f:
    print("Pointer at:", f.tell())
    f.read(5)
    print("Pointer after read:", f.tell())
Pointer at: 0
Pointer after read: 5
# Seek to start
with open("lines.txt", "r") as f:
    f.seek(0)
    print("Start:", f.read(6))
Start: Line 1
import os
print("Files in directory:", os.listdir())
Files in directory: ['.ipynb_checkpoints', 'age-calculator.ipynb', 'basics_py.ipynb', 'bmi_calculator.ipynb', 'dictionary_operations.ipynb', 'filehandling.ipynb', 'functions.ipynb', 'iris_knn_classifier.ipynb', 'lines.txt', 'list_vs_tuple.ipynb', 'loops_and_conditions.ipynb', 'matplotlib_demo.ipynb', 'numpy_basics.ipynb', 'oop.ipynb', 'python_operators.ipynb', 'quiz_app.ipynb', 'regex_basics.ipynb', 'safe.txt', 'sample.ipynb', 'sara.txt', 'temp.txt', 'test.ipynb']
# Check existence
print("Does sara.txt exist?", os.path.exists("sara.txt"))
Does sara.txt exist? True
# Get file size
print("Size of sara.txt:", os.path.getsize("sara.txt"))
Size of sara.txt: 21
# Rename file
os.rename("safe.txt", "renamed_safe.txt")
print("Renamed file!")
Renamed file!
# Delete file
if os.path.exists("temp.txt"):
    os.remove("temp.txt")
    print("Deleted temp.txt")
Deleted temp.txt
# Making new folder
if not os.path.exists("sara_folder"):
    os.mkdir("sara_folder")
    print("Folder created!")
Folder created!
# Writing inside folder
with open("sara_folder/newfile.txt", "w") as f:
    f.write("Inside folder!")
# Reading from folder
with open("sara_folder/newfile.txt", "r") as f:
    print(f.read())
Inside folder!
# Removing folder
os.remove("sara_folder/newfile.txt")
os.rmdir("sara_folder")
import csv
data = [["Name", "Score"], ["Sara", "1000"], ["Ali", "950"]]
with open("scores.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(data)
# Read CSV
with open("scores.csv", "r") as f:
    reader = csv.reader(f)
    for row in reader:
        print("CSV Row:", row)
CSV Row: ['Name', 'Score']
CSV Row: ['Sara', '1000']
CSV Row: ['Ali', '950']
# Read CSV
with open("scores.csv", "r") as f:
    reader = csv.reader(f)
    for row in reader:
        print("CSV Row:", row)
CSV Row: ['Name', 'Score']
CSV Row: ['Sara', '1000']
CSV Row: ['Ali', '950']
# Append to CSV
with open("scores.csv", "a", newline="") as f:
    writer = csv.writer(f)
    writer.writerow(["Afra", "890"])
# Reading again
with open("scores.csv", "r") as f:
    print(f.read())
Name,Score
Sara,1000
Ali,950
Afra,890
# Writing CSV using DictWriter
with open("dict_scores.csv", "w", newline="") as f:
    fieldnames = ["name", "marks"]
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({"name": "Sara", "marks": 99})
# Reading Dict CSV
with open("dict_scores.csv", "r") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row)
{'name': 'Sara', 'marks': '99'}
# CSV edge case: empty file
open("empty.csv", "w").close()
# Read empty
with open("empty.csv", "r") as f:
    print("Empty:", f.read())
Empty:
# Delete all CSVs
files = ["scores.csv", "dict_scores.csv", "empty.csv"]
for f in files:
    if os.path.exists(f):
        os.remove(f)
import json
info = {"name": "Sara", "skills": ["Python", "ML"], "score": 999}
with open("info.json", "w") as f:
    json.dump(info, f)
# Read JSON
with open("info.json", "r") as f:
    data = json.load(f)
    print("Loaded:", data)
Loaded: {'name': 'Sara', 'skills': ['Python', 'ML'], 'score': 999}
# Pretty JSON
with open("pretty.json", "w") as f:
    json.dump(info, f, indent=4)
# Load pretty
with open("pretty.json", "r") as f:
    print(f.read())
{
    "name": "Sara",
    "skills": [
        "Python",
        "ML"
    ],
    "score": 999
}
# Add to dict and dump
info["rank"] = 1
with open("info.json", "w") as f:
    json.dump(info, f)
# Load after update
with open("info.json", "r") as f:
    print(json.load(f))
{'name': 'Sara', 'skills': ['Python', 'ML'], 'score': 999, 'rank': 1}
# Check key existence
print("Has rank?", "rank" in info)
Has rank? True
# Delete key
del info["rank"]
print("After delete:", info)
After delete: {'name': 'Sara', 'skills': ['Python', 'ML'], 'score': 999}
# Re-dump JSON
with open("info.json", "w") as f:
    json.dump(info, f)
# Clean JSONs
for f in ["info.json", "pretty.json"]:
    os.remove(f)
# Recap: file write & read
with open("recap.txt", "w") as f:
    f.write("done")
# Read recap
with open("recap.txt", "r") as f:
    print(f.read())
done
# Check last files exist
print("Recap file?", os.path.exists("recap.txt"))
Recap file? True


Score: 45

Category: basics