Preview

13 - File Handling

 1. With most applications and programs it is clear that data needs to be stored after execution. This sort of storage is called:

  volatile memory

  random memory

  persistent storage

  temporary memory store

 2. The advantages of using storage that remains even after the 'execution' of the program is over include:

  All of the above

  An e-commerce website would be able to keep a record of your username and password

  If it is a game it would remember what level you were on

  If it is a game it would be able to retain and store the high scores and retreive them

 3. Most programming languages have their own methods that aid the storage of data and one example with Python is the use of 'pickle'
Note: We look at pickle in the advanced levels

  TRUE

  FALSE

 4. Which of the following is a good principle to remember when it comes to file handling?

  Be aware that if more than one program or user is trying to access the file at the same time, complications can arise

  It is good practice to check that the file exists, as otherwise your program will crash

  All of the above

  While coding, if you open a connection to a file, it is good practice to close the connection when no longer needed

 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.

  FALSE

  TRUE

 6. In the following program, what is the output?
fobj = open("myfile.txt")
for line in fobj:
    print(line.rstrip())
fobj.close()

  The contents of the text file 'myfile.txt' would be printed to the screen.

  Nothing, as 'r' (read mode) has not been specified

  "myfile.txt" is printed to the screen

  None of the above

 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":
fobj = open("myfile.txt")
for line in fobj:
    print(line.rstrip())
fobj.close()

  True

  False

 8. What needs to go where the '?' is in order to write the contents to the file 'myfile.txt'?
fh = open("myfile.txt", ?)
fh.write("To write or not to write\nthat is the question!\n")
fh.close()

  "W-R"

  "write"

  "X"

  "w"

 9. the command: close() will close an open file. It has no effect if the file is already closed.

  TRUE

  FALSE

 10. read(n) will read at most _________. Reads till end of file if it is negative or None.

  10 characters from the file

  only the first character from the file

  1 character from the file

   n characters from the file

 11. tell() …

  Write string s to the file and return the number of characters written.

  Write a list of lines to the file.

  returns the current file location

  Return an integer number (file descriptor) of the file.

 12. fileno()

  returns the current file location

  Return an integer number (file descriptor) of the file.

  Write a list of lines to the file.

  Write string s to the file and return the number of characters written.

 13. write(s)

  returns the current file location

  Write string s to the file and return the number of characters written.

  Write a list of lines to the file.

  Return an integer number (file descriptor) of the file.

 14. writelines(lines)

  Return an integer number (file descriptor) of the file.

  Write a list of lines to the file.

  returns the current file location

  Write string s to the file and return the number of characters written.

 15. Not all files contain just text. Some contain images. Valid file formats to which you can read and write from include:

  .csv

  .txt

  All of the above

  .xml and xls

 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)
import csv
with open('some.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

  FALSE

  TRUE

 19. What does the 'with open' do in reference to file handling?
import csv
with open('some.csv', newline='', encoding='utf-8') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

  All of the above are essentially saying the same thing -and are correct

  It is the alternative to file.open() and file.close() and is the preferred programming method to use

  with statements open a resource and guarantee that the resource will be closed when the with block completes, regardless of how the block completes

  It ensures that when you open a file and process its contents, it will automatically close the file too.

 20. t' ….

  opens a file for updating (readin g and writing)

  opens a file in text mode

  can create a new file if it does not exist

  opens a file in binary mode

 21. a'….

  opens a file in binary mode

  opens a file in text mode

  opens a file for updating (readin g and writing)

  can create a new file if it does not exist

 22. b'….

  opens a file for updating (readin g and writing)

  opens a file in binary mode

  opens a file in text mode

  can create a new file if it does not exist

 23. +'…

  opens a file in binary mode

  opens a file for updating (readin g and writing)

  can create a new file if it does not exist

  opens a file in text mode

 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.

  FALSE

  TRUE

 25. We need to be careful with the 'w' mode as it will ___________. All previous data are erased

  All of the above

  write several copies of text onto the file if the file is not closed at the end causing a crash

  crash the program if too much text is written to the program. 'write' (full version) is best used.

  overwrite into the file if it already exists