Test Index
File Handling in Python
© examsnet.com
Question : 36 of 37
Marks:
+1,
-0
(a) Write a point of difference between append
(a) and write (w) modes in a text file.Write a program in Python that defines and calls the following user defined functions :(i) Add_Teacher() : It accepts the values from the user and inserts record of a teacher to a csv file ‘Teacher.csv’. Each record consists of a list with field elements as T_id, Tname and desig to store teacher ID, teacher name and designation respectively.(ii) Search_Teacher() : To display the records of all the PGT (designation) teachers.OR(b) Write one point of difference between seek() and tell() functions in file handling. Write a program in Python that defines and calls the following user defined functions :(i) Add_Device() : The function accepts and adds records of the peripheral devices to a csv file ‘peripheral.csv’. Each record consists of a list with field elements as P_id, P_name and Price to store peripheral device ID, device name, and price respectively.(ii) Count_Device() : To count and display number of peripheral devices, whose price is less than ₹ 1000.[CBSE 2023 Compartment]
Solution:
(a)Difference:
a (append) mode opens the file to add new content at the end of the existing content; old data is kept (a new file is created if it does not exist). w (write) mode creates a new file for writing; if the file already exists, its old content is erased (overwritten).import csvdef Add_Teacher(): fout = open("Teacher.csv", "a", newline="") T_id = int(input("Enter Teacher id: ")) Tname = input("Enter Teacher name: ") desig = input("Enter Designation: ") rec = [T_id, Tname, desig] csvw = csv.writer(fout) csvw.writerow(rec) fout.close()def Search_Teacher(): fin = open("Teacher.csv", "r", newline="") csvr = csv.reader(fin) for record in csvr: if record[2] == "PGT": print(record) fin.close()Add_Teacher()Search_Teacher()Explanation: Add_Teacher() opens the file in append mode so that earlier records stay, and writerow() writes the list as one row. Search_Teacher() reads the rows with csv.reader() (each row is a list of strings) and displays rows whose designation, record[2], is PGT.OR (b)Difference: seek(offset) moves the file pointer to the given position (byte) in the file, e.g. f.seek(0) goes to the beginning; tell() only returns the current position of the file pointer as an integer and does not move it.import csvdef Add_Device(): fout = open("peripheral.csv", "a", newline="") P_id = int(input("Enter Device Id: ")) P_name = input("Enter Device name: ") Price = int(input("Enter Price: ")) rec = [P_id, P_name, Price] csvw = csv.writer(fout) csvw.writerow(rec) fout.close()def Count_Device(): fin = open("peripheral.csv", "r", newline="") csvr = csv.reader(fin) ctr = 0 for record in csvr: if int(record[2]) < 1000: ctr = ctr + 1 print("Number of devices with price less than 1000:", ctr) fin.close()Add_Device()Count_Device()Explanation: Values read from a csv file are strings, so the price record[2] is converted with int() before comparing it with 1000; the counter is increased for each such device and displayed at the end.© examsnet.com
Go to Question: