CBSE Class 12 Computer Science
Question 110 of 136
Data File Handling — Question 49
Back to all questionsThe code asks the user to enter a name. It then searches for the name in "contacts.csv" file. If found, the name and phone number are printed as the output.
name = input("Enter name :")— This line prompts the user to enter a name through the console, and the entered name is stored in the variablename.file = open("contacts.csv", "r")— This line opens the filecontacts.csvin read mode and assigns the file object to the variablefile.for line in file:— This line initiates a for loop that iterates over each line in the file handle (filerepresents the opened file object), which enables interaction with the file's content. During each iteration, the current line is stored in the variableline.if name in line:— Within the loop, it checks if the inputted name exists in the current line using theinoperator.print(line)— If the name is found in the current line, this line prints the entire line to the console.