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

Remove Ads
Teachoo · Class 12 Explore Class 12

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 is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.