File Handling Questions
File Handling Questions
VIVA QUESTIONS
1. What is the difference between files and variables of other data
types(like list, dictionary etc.) in terms of data storage ?
Variables store data in RAM which remains in memory as long as the program
is running and gets erased the moment our program gets over. This happens
because RAM is volatile in nature.
Files store the data on secondary storage devices like hard disk. The data stored
on secondary storage devices is permanent in nature and does not get erased
when the program gets over. When input/output operations are performed on
files data gets transferred from secondary storage to RAM and vice versa.
1
3. What is the difference between a and w modes?
2
'w' Open a file for writing. Creates a new file if it does not exist or
truncates the file if it exists.
'a' Open for appending at the end of the file without truncating it.
Creates a new file if it does not exist.
Using with ensures that all the resources allocated to file objects gets
deallocated automatically once we stop using the file.
The write() function write a single string at a time and writelines() methods can
be used to write a sequence of strings.
The readlines()method reads the entire content of the file in one go and
returns a list of lines of the entire file. This method returns an empty value
when an end of file (EOF) is reached.
3
• Unpickling is the process by which a byte stream is converted back into the
desired object.
The process of pickling and unpickling a Python data object is known as object
serialization.
10. Name the methods which are used to write and read into/form a
binary file.
The dump method of pickle module is used to write objects to binary file. The
load method of pickle module is used to read the object from the binary file.
11. Name two important functions of CSV module which are used for
reading and writing.
The two important functions of CSV module are:
csv.reader() returns a reader object which iterates over lines of a CSV file
returns a writer object that converts the user's data into a
csv.writer() delimited string. This string can later be used to write into CSV
files using the writerow() or the writerows() function.