Preview

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:
OPTION #1
(1) output the variable
(2) assign its contents to a variable; 
(3) open this file for reading;

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

  option 2

  option 1

  neither option is referring to file handling

  either option 1 or 2 would be work

 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
WHILE NOT myFile.EOF
     OUTPUT myFile.READLINE()
END WHILE
myFile.CLOSE()

  End of File / characters

  Expected operative field / integers

  Expected operation on file / strings

  End of File / index numbers

 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 ________.
uploads/file_integer_columns.png

  integers / executing

  integers / casting

  strings / concatenation

  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:")
        Writer.writerow([id,firstname])
        print("Record has been written to file")

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

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

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

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

 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 / internally / string / integer

  file / externally /text / binary

  variable / internally / string / text

 6. Have a look at the description below and fill in the blanks where appropriate. (lines 7, 9 and 10)
>>> my_file.open('my_filename','w')

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

  appending / write / read

  read only / appending / write

  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(f.read().split())

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

  counts the frequency of words in a file.

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

  counts every character for every word in the file

  counts the number of words in the whole 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 / left and right / right and end

  leading / trailing / trailing / trailing

  leading / trailing / leading / trailing

  trailing / heading / leading / leading

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

file_read("test.txt")

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

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

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

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

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

  reads a random word from the file

  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