Student Activity
Student Activity Guide:
1) Reading and Writing Files (Text, CSV, JSON)
# Writing to a text file
with open("example.txt", "w") as file:
file.write("Hello, World!
Python File Handling")
# Reading a text file
with open("example.txt", "r") as file:
content = file.read()
print(content)import csv
# Writing to a CSV file
with open("data.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Name", "Age", "City"])
writer.writerow(["Alice", 25, "New York"])
# Reading a CSV file
with open("data.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)2) Working with File Paths and Directories
3) Error Handling in File Operations
Last updated