1. With most applications and programs it is clear that data needs to be stored after execution. This sort of storage is called:
2. The advantages of using storage that remains even after the 'execution' of the program is over include:
3. Most programming languages have their own methods that aid the storage of data and one example with Python is the use of 'pickle'
4. Which of the following is a good principle to remember when it comes to file handling?
5. Data is sometimes written to, and read from, files as a string – this may mean that you need to convert the datatype of your input and output in order to use them as intended in your program.
6. In the following program, what is the output?
7. In the following example the method rstrip() is used to strip off whitespaces (newlines included) from the left and right side of the string "line":
8. What needs to go where the '?' is in order to write the contents to the file 'myfile.txt'?
9. the command: close() will close an open file. It has no effect if the file is already closed.
10. read(n) will read at most _________. Reads till end of file if it is negative or None.
15. Not all files contain just text. Some contain images. Valid file formats to which you can read and write from include:
16. In the following code, what is the 'a'?
with open('studentfile.txt','a') as studentfile:
studentfileWriter=csv.writer(studentfile)
studentfileWriter.writerow([id,firstname,surname,dob,firstlineaddress,postcode,gender,tutorgroup,email])
print("Record has been written to file")
studentfile.close()
menu()
Open file in 'append' mode (add to the existing entries, don't overwrite)
Overwrite the file with the new contents 'a'
All of the above
Write 'a' to file
17. The following code makes use of a csv.writer. Which statement is true about CSV files and python's CSV module?
def passwordchecker():
password=input("Please enter a password - must be secure and meet our format requirements")
if test_password(password):
with open('fakeflixfile.txt','a') as fakeflixfile:
fakeflixfileWriter=csv.writer(fakeflixfile)
fakeflixfileWriter.writerow([username,password,firstname,surname,dob,firstlineaddress,postcode,gender,interest,email])
print("Record has been written to file")
fakeflixfile.close()
menu()
else:
passwordchecker()
The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases
The csv module implements classes to read and write tabular data in CSV format
The csv module’s reader and writer objects read and write sequences. (e.g. read/write data preferred by Excel)
All of the above
18. The following shows the simplest example of reading a CSV file (from Python documentation)
19. What does the 'with open' do in reference to file handling?
24. It is good, but not necessary, to close a file, as the resources that were tied up with the file are closed down automatically anyway.
25. We need to be careful with the 'w' mode as it will ___________. All previous data are erased