Download presentation
Presentation is loading. Please wait.
1
File Handling
2
File Handling - #recap Procedure structure: def menu(): enter code for print, input, etc.. menu() #calls up code from above if not included code will not run #Remember it is good practice to include all procedures at the top of your code and then call them later.
3
Create a menu #Create a Menu using print (and an input) for the following: ****Film details**** 1…..Create to film file 2…..Read data from file 3…..Append film to file Please enter selection: _ #Use procedures for each option and an if elif to call the correct procedure depending on the user’s selection.
4
File Handling – writing to file ‘wt’
#writing to file, this should be in a procedure (e.g. def write file():) myFile = open(‘films.txt', 'wt') myFile.write (‘Star Wars\n') #add extra films here myFile.close()
5
File Handling – reading from file ‘rt’
#reading from file – use a procedure: #reading contents a line at a time myFile = open(‘films.txt', 'rt') for line in myFile: #for loop to read a line at a time print(line) myFile.close()
6
File Handling – appending to file ‘a’
#enter the code below in another procedure: myFile = open(“films.txt", "a") film = input(“Please enter name of film to append: ”) myFile.write(film) print(film, “has been added to the file”) myFile.close()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.