13 - File Handling

 1. Assume we had a simple text file - test.txt - that contains a phrase: "Hello outside World". To get this string information into a program, we need to do things in the following order:
(1) output the variable
(2) assign its contents to a variable; 
(3) open this file for reading;

(1) open this file for reading; 
(2) assign its contents to a variable; 
(3) output the variable.

  neither option is referring to file handling

  either option 1 or 2 would be work

  option 1

  option 2

 2. Analyse the pseudocode below and fill in the blanks. EOF is the _________________ which is a condition for the while loop to stop reading the file's lines. Readline reads ___________ in the file until it hits EOL which is the End of Line delimiter
myFile := OPEN ("test.txt") FOR READING

  Expected operative field / integers

  End of File / index numbers

  End of File / characters

  Expected operation on file / strings

 3. Columns 2 to 4 are __________ and need to be converted before we can find out the average or any other statistic on it. Text files are in string format and need string manipulation and ________.

  integers / executing

  strings / concatenation

  integers / casting

  strings / casting

 4. What is the following code achieving?
with open('fakefacebook_write.txt','a',newline="") as fo: 
	#open the file in _______ mode (add to file, we don't wish to overwrite!)
        Writer=csv.writer(fo) #fo = file out (this can be called anything you like)
        id=input("Enter ID:")
        firstname=input("Enter firstname:")
        print("Record has been written to file")

  opens the file and appends the words 'writer' and 'writerow' to the end of the file

  writes id and firstname to the file over writing any other data in the file

  asks the user for ID number and first name, and reads it from the file

  asks the user to enter an ID number and first name, and writes it to the file (appends)

 5. Read the paragraph below that presents a simple but helpful introduction to files and see if you can fill in the blanks.
A ____ is some information or data which isn't lost when you switch off your 
computer! It is stored ___________ in your computer storage which means it
can be accessed whenever you want. You already know about different kinds
of file , like your music files, video files, text files. You can only 
imagine the sort of massive "files" that an organisation like facebook 
holds on each of us! Python gives you easy ways to manipulate these files. 
Generally we divide files in two categories, _____ file and _______ file. 
Text files are simple text where as the binary files contain 
binary data which is only readable by computer.

  file / internally / text / string

  file / externally /text / binary

  file / internally / string / integer

  variable / internally / string / text

 6. Have a look at the description below and fill in the blanks where appropriate. (lines 7, 9 and 10)

In this example a variable (myfile) is created and then open is used 
to create a file object with 2 arguments. The first is a string with
the filename and the second is the mode to be used. This can be:

r(default if not specified) _________
w - write
a open for __________ only
r+ - read and ______

  append only / reading / write

  read only / appending / write

  appending / write / read

  read only / write / write

 7. Try it yourself if you are not sure. Play around with the text file as well and see what the output does. What does the following program do?
from collections import Counter
def word_count(fname):
        with open(fname) as f:
                return Counter(

print("Number of words in the file :",word_count("test.txt"))

  counts the number of files present that contain a certain number of words

  counts the number of words in the whole file

  counts the frequency of words in a file.

  counts every character for every word in the file

 8. Play with the trinket and follow the comment's instructions
Fill in the blanks
.strip(): will remove ________ and ______ characters
.lstrip():  will remove ______characters
.rstrip(): will remove _______ characters

  leading / trailing / leading / trailing

  leading / trailing / left and right / right and end

  trailing / heading / leading / leading

  leading / trailing / trailing / trailing

 9. What is line 3 and Content_list doing?
def file_read(fname):
        with open(fname) as f:
                content_list = f.readlines()


  'content_list' is not a list but a file that acts as a list

  'content_list' is the list that contains the read lines.

  'content_list' is the file that the list is read from

  'content_list' is a list that is created inside the file

 10. Play around with the trinket below. What is this program doing?

  reads the file randomly, using multiple files to read from

  reads a random line from the file and displays it

  reads a random character from the file

  reads a random word from the file