File Handling
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.
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.
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()
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()
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()