Python File Handling Notes Class12
Python File Handling Notes Class12
What is a File?
A file is used to store data permanently on a computer. Python can open, read, write, and close files.
Types of Files
File Paths
'r': Read
'a': Append
data = file.read()
file.write('Hello')
file.writelines(['Line1\n', 'Line2\n'])
Python File Handling Notes - Class 12
file.read() - All
file.seek(0) - Go to start
import pickle
pickle.dump(obj, f)
obj = pickle.load(f)
CSV Files
Writing to CSV
import csv
Python File Handling Notes - Class 12
writer = csv.writer(f)
writer.writerow(['Name', 'Age'])
reader = csv.reader(f)
print(row)