Q:

Python program to write data to file

0

Write Operation is the process of writing data to file by the program. This is done using the write() method available in the Python library.

The Python program is opened in 'w' mode, to write files.

Algorithm to Write to File in Python:

  • Step 1: Open the file using 'w' mode. (if the file is not present the write mode will create a new one).
  • Step 2: Get input from the user.
  • Step 3: User the write() method to write the contents to the file.
  • Step 4: Close the file.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python program to write data to a file

F=open("drinks.dat","w")

while(True):
    v=input("Enter Drink Name : ")
    
    if(v==""):
        break
    
    F.write(v+"\n")

F.close()

Output:

Enter Drink Name : RedBull
Enter Drink Name : Cafe Mocha
Enter Drink Name : Americano
Enter Drink Name : Coca Cola
Enter Drink Name : Tea 
Enter Drink Name : 

File : drinks.dat 
RedBull
Cafe Mocha
Americano
Coca Cola
Tea 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now