0% found this document useful (0 votes)
14 views3 pages

File Handling Ques-Ans

Uploaded by

masterayaan981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

File Handling Ques-Ans

Uploaded by

masterayaan981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FILE HANDLING QUESTION ANSWERS

Q.1. What are files in Python?


Ans. A file is a named place on the disk where a sequence of related data is stored. In
python files are simply stream of data, so the structure of data is not stored in the file.

Q.2. How many types of files can you make in Python?


Ans. Python allows us to create two types of file – Text File and Binary File.

Q.3. What is the difference between Text File and Binary File?
Ans.
TEXT FILE BINARY FILE

Data is stored in ASCII format that is Data is stored in binary form (0s an 1s), that
human readable. is not human readable.
It has an EOL character that terminates It stores data as a stream of bytes.
each line of data.
We can read and write from/to a text file Conversion of data from machine readable
directly using built-in functions.. form to human readable form is required
before reading and writing in binary file.

Q.4. What is meant by File Access Mode?


Ans. A file access mode describes how file will be used throughout the program. The default
access mode is ‘r’ (reading).

Q.5. What is the difference between read(), readline() and readlines() function.
Ans.
read() readline() readlines()
This method reads the entire This method reads a line as This method returns the
content of the file. string and returns it. entire content of the file.
This method reads the file This method also reads the This method returns a list of
data and returns a string. new line character. strings, each separated by
It returns an empty string It returns an empty string \n.
when end of file is reached. when end of file is reached. It returns an empty string
when end of file is reached.
read(size) This method reads
specific quantity of data.
If the size is not provided or
a negative value is provided
then entire file will be read.
Q.6. What is the difference between write() and writelines() function.
Ans.
write() writelines()
This method is used to write string type This method is used to write sequence
data in a text file. To store numeric values data type such as list, tuple, etc. in a text
we have to convert it into string type. file. It can also be used to write strings.

Q.7. How can we access a text file randomly or non-sequentially.


Ans. Seek() function is used to access the file contents randomly.

Q.8. What is the difference between seek() and tell()?


Ans.
SEEK() TELL()
It is used to position the file object at a It returns an integer giving the current
particular place in the file. position of object in the file.
Its syntax is – Its syntax is –
<file object>.seek(<offset value>, <file object>.tell()
<from_what>)

Q.9. What are the valid from_what values used with Seek() function. Also, state the
reference point they refer to.
Ans. The from_what values used with Seek() function are 0,1,2.
Here, 0 refers to the beginning of the file (default value),
1 refers to the current position of file and
2 refers to the end of the file.
NOTE : Some valid examples of seek () function
f.seek(20,0) f.seek(-10,2) f.seek(20,1) f.seek(-20,1)
Some
invalid examples of seek () function
f.seek(-20,0) f.seek(10,2)
Q.10. What is Pickle module?
Ans. Pickle module is used in serialization of data. This allows us to store data in binary form
in the file. It uses dump and load functions write data and read data from binary file.
Pickle module allows to store any kind of object in file as it allows to store python
objects with their structure.

Q.11. What is Serialization?


Ans. It is the process of converting a data structure / object that can be stored in non-string
format and can be resurrected later. It can also be called as deflating data and resurrecting
as inflating it.
Q.12. What is the advantage of using with clause while opening a data file in Python? Also
give syntax of with clause.
Ans. With statement is used to create or open a file for use. The advantage of using with
clause is that it ensures that all the resources allocated to file object gets deallocated once
we stop using the file. Its syntax is :
with open(<file name> , <access mode>) as <file object> :
< file manipulation statements>

Q.13. Differentiate between ‘w’ and ‘a’ file modes in Python.


Ans.
‘w’ ‘a’
‘w’ represents write mode. It opens the ‘a’ represents append mode. It opens
file for writing new data. the file for adding more data in the file.
In this the file pointer is placed at the In this the file pointer is placed at the
beginning of the file. end of the file.

Q.14. What is the use of close() function?


Ans. close() function free up all the system resources used by the file. This means once a file
is closed, we will not be able to use the file object any more.

Q.15. What happens if we reassign a file object to some other file, without closing the file.
Ans. When we reassign a file object to some other file, then python will automatically close
the file.

You might also like