0% found this document useful (0 votes)
13 views

File Handlingquestions and Answers

Python ²

Uploaded by

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

File Handlingquestions and Answers

Python ²

Uploaded by

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

FILE HANDLING

(Questions and Answers)


Question : 01
Differentiate between the following
(i) fp =open(“emp.dat” , ‘r’)
(ii) fp =open(“emp.dat” , ‘w’)

Ans:-
(i) File has been opened in read mode with file pointer(object) fp

(ii) File has been opened in write mode with file pointer(object) fp
Question : 02
What is the difference between readline() and readlines()

Ans:-
readline() : - The readline() function is used to read a line at a time from the
file and retured the data in form of string.

readlines() : - While the readlines() function reads all the lines from the file
and return a list of all the lines.
Question : 03
Read the code given below and answer the questions:
f1 = open(“info.txt”, “w”)
f1.write(“Thank You”)
f1.close()

If the file “info.txt” contains “ Hello” before execution of above code, then what will
be the content of the file after the execution of this code?

Ans:-
The file would now contain “Thank You” only. Because when an existing
file is opened in write mode(i.e.’w’) then it truncate/removes all the existing
data from the file.
Question : 04
Identify the error in the following code:

import pickle
data = [‘one’,2,[3,4,5]]
with open(“info.dat”, “rd”) as fp:
pickle.dump(data,f)

Ans:-
In the above code there are two errors:-
(i) As you can see in the above code the file is opened in reading mode
and dump() function is used to write the data in a file. Hence the file
should be opened in writing mode i.e. the Line-3 should be
with open(“infor.data”, “wb”) as fp:
(ii) In Line-4 the file pointer/object name should be fp not f
Question : 05
Consider the file “emp.txt” then write the code to print the last line from the file.

Ans:-

f1 = open(“emp.txt” ,”r”)
L = f1.readlines()
print(“Last line of the file:”, L[-1])
Question : 06
Consider the following statemenmt. In which file mode is the file opened ? Justify
your answer.

with open(“stud.txt”) as fp:

Ans:-
The file “stud.txt” is opened in read mode because the file mode has not
been specified in the above code and by default the file mode is opened in
read mode.
Question : 07
Write a method/function in python to read the content from a text file “story.txt”
line by line and display the sdam on the screen

Ans:-

def read_data():
f1 = open(“story.txt” ,”r”)
for ln in f1:
print(ln)
f1.close()

Note:-
We can also use while loop to read data line by line. See the programs
written in “File Handling Part-V”
Question : 08
Explain the following terms
(i) Pickling
(ii) Unpickling

Ans:-
Question : 09
Define a function display_line() to display all the lines of the file “data.txt” along
with line no. on the screen

Ans:-

def display_line():
fp = open("data.text","r")
c =1
ln=fp.readline()
while ln:
print(c,": ", ln)
ln = fp. readline()
c = c+1
fp.close()
Question : 10
Define a function display_data() that read lines form the file “student.text” and
display those lines which are starting with alphabet ‘T’

def display_data():
fp = open(“student.text","r")
ln=fp.readline()
while ln:
if(ln[0]==‘T’):
print(ln)
ln = fp.readline()
fp.close()

You might also like