KnowledgeBoat Logo

Computer Science

What is the following code doing ?

file = open("contacts.csv", "a")
name = input("Please enter name.")
phno = input("Please enter phone number.") 
file.write(name + "," + phno + "\n")

Python File Handling

3 Likes

Answer

This code opens a CSV file named contacts.csv in append mode, as indicated by the mode "a". This mode allows new data to be added to the end of the file without overwriting existing content. It then prompts the user to enter a name and a phone number through the console using the input() function. After receiving input, it concatenates the name and phno separated by a comma, and appends a newline character '\n' to signify the end of the line. Finally, it writes this concatenated string to the CSV file using the write() method of the file object. This operation effectively adds a new record to the CSV file with the provided name and phone number.

Answered By

3 Likes


Related Questions