Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

File Handling Questions

File Handling Viva Questions

Uploaded by

Hema Chaudhry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

File Handling Questions

File Handling Viva Questions

Uploaded by

Hema Chaudhry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FILE HANDLING

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.

2. Compare Text, Binary and CSV files.

Text files Binary files CSV files


Text files are Binary files deal with CSV (Comma Separated
sequence of lines, non-text files such as Values) is a
where each line images or exe and store simple file format used
includes a sequence of data in binary format to store tabular data.
characters. All lines i.e. in the form of 0’s They are a convenient
are terminated by a and 1’s which is way to export data from
special character, understandable by the spreadsheets and
called the EOL or End machine. So when we databases as well as
of Line character. open the binary file in import or use it in other
Text files are stored in our machine, it decodes programs. These files are
human readable form the data and displays in a saved with the .csv file
and they can also be human-readable format. extension. In general, the
created using any text separator character is
editor. called a delimiter, and
other popular delimiters
include the tab (\t), colon
(:) and semi-colon (;)
characters.
Examples of text files Binary files can range
are: Web from image files like
standards: html, XML, JPEGs or GIFs,
CSS, JSON etc. audio files like MP3s
Source code: c, app, or binary document
js, py, java etc. formats like Word or
Documents: txt, tex, PDF.
RTF etc.

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.

4. What is the difference in opening a file using open() function or


using with statement?

Using with ensures that all the resources allocated to file objects gets
deallocated automatically once we stop using the file.

5. What is the need of closing files?


Closing a file frees up the resources that were tied with the file. Before closing a
file, any material which is not written in file, is flushed off i.e., written to file.
So, it is a good practice to close the file once we have finished using it.

6. What is the difference between the write() and writelines() methods


used to write data in a file?

The write() function write a single string at a time and writelines() methods can
be used to write a sequence of strings.

7. What is the difference between the read() and readlines() methods


used to read data from a file?
The readline() method reads one line(i.e. till newline) at a time from a file and
returns that line. It reads the file till newline including the newline character.
The file is read sequentially from top i.e. first call to readline() method returns
first line, second call returns second line till end of file. The readline() method
returns an empty string when the end of file is reached.

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.

8. What is the purpose of read(n) method?


This method reads a string of size (here n) from the specified file and returns it.
If size parameter is not given or a negative value is specified as size, it reads and
returns up to the end of the file. At the end of the file, it returns an empty string.

9. What do you understand by the terms pickling and unpickling?


To store data in binary files, Pickle module is used. Pickle module is used to store
any kind of object in file as it allows us to store python objects with their structure.
Pickle module supports two major operations: Pickling and Unpickling.

• Pickling is the process whereby a Python object is converted into a byte


stream.

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.

You might also like