Computer Science - Class 12
Chapter 2 - File Handling in Python

Create file phonebook.dat that stores the details in

following format: 

Name Phone

Jivin 86666000 

Kriti 1010101 

Obtain the details from the user.

 

Answer:

f=open( "phonebook.dat" , 'w' )

f.write( "Name Phone\n" )

while True:

    name=input( "Enter name:" )

    phone=input( "Enter phone number: " )

    f.write(name+ " " +phone+ "\n" )

    more=input( "Want to enter more? Enter Y if yes: " )

    if more.upper() != 'Y' :

        break

f.close()

Question 3-Create file phonebook - Teachoo.JPG

File phonebook.dat:

image2.png

Go Ad-free

Transcript

Question 3 Create file phonebook.dat that stores the details in following format: Name Phone Jivin 86666000 Kriti 1010101 Obtain the details from the user. Opens file phonebook.dat in write mode Accepts user input ‘name’ and ‘phone’ and writes it to the file Output Writes data to the file Closes the file

Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo