Class: XII
Chapter-5File Handling
Assignment
1. What is the difference between “w” and “a” modes ?
Ans:
“w” - If file exists, Python will truncate existing data and over-write in the file.
“a” – If the file exists, the data in the file is retained and new data being written
will be appended to the end.
2. What is the significance of file-object?
Ans: A file-object is a reference to a file on disk. It opens and makes it available
for a number of different tasks.
Ex: f=open(r”C:\temp\[Link]”,”r”)
Here , f is the file-object created. [Link]( ), [Link]( ), [Link]( ), [Link]( ),
[Link]( ), [Link]( ) etc., are the different tasks performed through file object.
3. How is the file open( ) function different from close( ) function?
Ans:
The open( ) function is a built in function and it takes two parameters; filename
and mode. open( ) function returns a file [Link]. f=open(“C:\\[Link]”,”r”)
close( ) is a method used with file handle object as [Link]( )
4. Write statements to open a binary file C:\Myfiles\[Link] in read and write
mode by specifying the file path in two different formats.
i. f=open(r” C:\Myfiles\[Link]”,”rb+”)
ii. f=open(” C:\\Myfiles\\[Link]”,”wb+”)
5. When a file is opened for output ,what happens when
i)The mentioned file does not exist ii) The mentioned file does exist?
In “r” mode:
i. If the mentioned file does not exist, it raises FileNotFoundError.
ii. If exist, then it is ready to read
In “w” mode:
i. If the file does not exist, file is created.
ii. If exist, it will overwrite the content.
In “a” mode:
i. If the file doesn’t exist, Python will create a new file.
ii. If exist, the content will be retained and new data will be added at the end.
Class: XII
6. What role is played by file modes in file operations? Describe the various file
mode constants and their meanings.
Text Binary Description Notes
File File
mode mode
‘r’ ‘rb’ Read only File must exist already, otherwise Python raises I/O
error.
‘w’ ‘wb’ Write only If the file does not exist, file is created.
If the file exists, python will truncate existing data
and overwrite in the file. So this mode must be used
with caution.
‘a’ ‘ab’ Append File is in write only mode
If the file exists, the data in the file is retained and
new data being written will be appended to the end.
If the file does not exist, python will create a new
file.
‘r+’ ‘r+b’ Read and File must exist otherwise error is raised.
or write Both reading and writing operations can take place.
‘rb+’
‘w+’ ‘w+b’ Write and File is created if does not exist.
or read If file exists, file is truncated (past data is lost).
‘wb+’ Both reading and writing operations can take place.
‘a+’ ‘a+b’ Write and File is created if does not exist.
or read If file exists, file’s existing data is retained. New
‘ab+’ data is [Link] reading and writing
operations can take place.
7. What are the advantages of saving data in : i) binary form , ii)text form?
Binary form Text Form
1. Uses less memory [Link] text
2. Binary files are more efficient [Link] need for any syntax to be defined
3. It works faster [Link] can be ported to different type of
systems.
4. The binary files store information 4. Stores information in ASCII or
in the same format in which the Unicode characters where each line is
information is held in terminated with an EOL. (Internal
[Link]., the file content that translation takes place in Text file)
is returned is raw(with no
translation or no specific
Class: XII
encoding)
8. When do you think text files should be preferred over binary files?
When file does need to be read by people or need to be ported to a different type
of system, text files should be preferred over binary files.
9. Write a statement in python to perform the following operations:
a) To open a text file “[Link]” in read mode
b) To open a text file “[Link]” in write mode
Ans:
i. f=open(“[Link]”,”r”)
ii. f=open(r” C:\[Link]”,”w”)
10. Define Absolute and Relative pathnames of Python.
The absolute paths are from the topmost level of the directory structure. The
relative paths are relative to current working directory denoted as a dot(.) while its
parent directory is denoted with two dots(..).
11. Write the differences between read(), readline(), readlines(). Explain it with
this example. Consider the following lines of “[Link]”
The Dowry system is evil in society.
It has reduced the sacred affair of marriage to a business deal.
Brides are treated as a marketable commodity.
The parents of the brides are often put under inhuman pressure for a handsome
dowry.
Ans:
a) read() – Reads at most n bytes. If no n is specified it will read the entire file
e.g.
f1=open(“E:\\mydata\\[Link]”)
info=[Link](15)
output:
The Dowry syste
b) readline() – Reads a line of input. If n is specified it reads at most n bytes.
It returns read bytes in the form of a string ending with line character or returns a
blank string
if no more bytes are left for reading in the file.
e.g.
f1=open(“E:\\mydata\\[Link]”)
Class: XII
info=[Link]()
output:
The Dowry system is evil in society.
c) readlines() – Read all lines and returns them in a list
e.g.
f1=open(“E:\\mydata\\[Link]”)
info=[Link]()
output:
[“The Dowry system is evil in society.\n”” It has reduced the sacred affair
of marriage to a business deal.\n”“Brides are treated as a marketable
commodity. \n””The parents of the brides are often put under inhuman
pressure for a handsome dowry.\n“]
[Link] are delimited text files? Give examples
Text files where a particular character is stored to separate the data in it are
known as delimited text files. In these [Link] will be a tab (→) , comma(,) ,
pipe(│) or tilde(~ ) placed after each value.
E.g.: CSV files – Comma Separated Files
TSV files – Tab separated files
[Link] is the difference between write() and writelines()?
Write( ) method displays the output but do not provide a new line character.
WriteLines( ) method displays the output and also provides a new line character at the
end of the
string, This would set a new line for the next output.