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

  persistent storage

  random memory

  temporary memory store

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

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

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

  All of the above

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

 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

  FALSE

  TRUE

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

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

  All of the above

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

  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()

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

  "myfile.txt" is printed to the screen

  The contents of the text file 'myfile.txt' would be 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()

  "write"

  "w"

  "W-R"

  "X"

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

  FALSE

  TRUE

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

  10 characters from the file

  1 character from the file

   n characters from the file

  only the first character from the file

 11. tell() …

  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.

 12. fileno()

  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.

  returns the current file location

 13. write(s)

  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.

 14. writelines(lines)

  Write a list of lines to the file.

  Return an integer number (file descriptor) of 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:

  All of the above

  .xml and xls

  .csv

  .txt

 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()

  Overwrite the file with the new contents 'a'

  Write 'a' to file

  Open file in 'append' mode (add to the existing entries, don't overwrite)

  All of the above

 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 csv module’s reader and writer objects read and write sequences. (e.g. read/write data preferred by Excel)

  The csv module implements classes to read and write tabular data in CSV format

  The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases

  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 ensures that when you open a file and process its contents, it will automatically close the file too.

  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

 20. t' ….

  can create a new file if it does not exist

  opens a file in text mode

  opens a file for updating (readin g and writing)

  opens a file in binary mode

 21. a'….

  can create a new file if it does not exist

  opens a file in binary mode

  opens a file in text mode

  opens a file for updating (readin g and writing)

 22. b'….

  opens a file for updating (readin g and writing)

  opens a file in binary mode

  can create a new file if it does not exist

  opens a file in text mode

 23. +'…

  opens a file in binary mode

  can create a new file if it does not exist

  opens a file for updating (readin g and writing)

  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

  overwrite into the file if it already exists

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

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