I/O (Input/Output) in Python
1. File Objects
Definition: A file object represents an open file and provides methods and attributes to interact with the file’s content.
2. Creating a File Object
Syntax:
file_object = open(filename, mode)filename: String path of the filemode: Specifies file operation'r'– read (default)'w'– write (creates/overwrites file)'a'– append'b'– binary mode (can be combined with other modes, e.g.,'rb')'+'– read and write
Example:
f = open("example.txt", "r")3. Reading File Contents
Methods:
read()– reads entire filereadline()– reads one line at a timereadlines()– reads all lines as a list
Example:
with open("example.txt", "r") as f:
content = f.read()
print(content)4. Writing Data into a File
Methods:
write(string)– writes a string to filewritelines(list_of_strings)– writes multiple strings
Example:
with open("output.txt", "w") as f:
f.write("Hello, world!\n")
f.writelines(["Line 1\n", "Line 2\n"])5. Reading and Writing CSV Files
Python’s csv module supports reading and writing CSV files, which are commonly used for tabular data.
Reading CSV:
import csv
with open('data.csv', mode='r', newline='') as file:
reader = csv.reader(file)
for row in reader:
print(row) # each row is a list of stringsWriting CSV:
import csv
data = [
['Name', 'Age', 'City'],
['Alice', '30', 'New York'],
['Bob', '25', 'Los Angeles']
]
with open('output.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)6. Using with Clause
Definition:
with statement is a context manager that ensures proper acquisition and release of resources, such as automatically closing files after usage.
Example:
with open("file.txt", "r") as f:
data = f.read()
# file is automatically closed here7. Using Exception Handling with File Operations
File operations can raise exceptions (e.g., FileNotFoundError). These should be handled gracefully.
Example:
try:
with open("nonexistent.txt", "r") as f:
content = f.read()
except FileNotFoundError:
print("File not found!")These detailed notes cover basics of file handling, safe file opening/closing, reading/writing text and CSV files, and handling exceptions in Python I/O.
This completes the last unit as per your syllabus. If needed, more elaborations or additional example sets can be provided.