2.2.3 Additional programming techniques Flashcards
What is File Handling?
File Handling is a way of storing information
What is the code to open a file?
file = open (“document.txt” , “w”)
file - keyword, assign to operation
open - reserve word to open a document
document - document name
w - operation
What does w, a and r mean?
w - write
a - append
r - read
What is the code so it will write your name and age into the file?
file.write (name)
file.write (age)
What is the code to close the document?
file.close()
What can you not write into a text file?
int
Ask 10 students to enter their preferred meal for parents evening
for i in range (10):
name = str(input(“What is your name?”))
meal = str(input(“Do you want rice, chicken, pasta or meatballs?”))
file = open(“meal.txt” , “a”)
file.write (name + “,”)
file.write (meal)
file.write (“\n”)
file.close ()
What does file.write (“\n”) mean?
It means new line and you have to apply it to the end