File Handling
File Handling
6. To read the entire remaining contents of the file as a string from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
8. To read the next line of the file from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
9. To read the remaining lines of the file from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
11. Which is a built-in function to read a line of text from standard input, which by default comes from the
keyboard?
a) input
b) input & scan
c) scan & scanner
d) scanner
Attribute Description
file.closed Returns true if file is closed, false otherwise.
file.mode Returns access mode with which file was opened.
file.name Returns name of the file.
file.softspace Returns false if space explicitly required with print, true otherwise.
import sys
print 'Enter your name: ',
name = ''
while True:
c = sys.stdin.read(1)
if c == '\n':
break
name = name + c
print (name)
If entered name is
sanfoundry
a) sanfoundry
b) sanfoundry, sanfoundry
c) San
d) None of the mentioned
25. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) all of the mentioned
d) none of the mentioned
34. Which of the following is modes of both writing and reading in binary format in file.?
a) wb+ or w+b
b) w
c) wb
d) w+
38. How do you get the current position within the file?
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos
41. How do you change the file position to a byte value from the start?
a) fp.seek(byte, 0)
b) fp.seek(byte, 1)
c) fp.seek(byte, 2)
d) none of the mentioned
46. Which method is used to convert Python objects for writing data in binary file?
a. write()
b. load()
c. store()
d. dump()
48. Which of the following function is used to read the data in binary file?
a. read()
b. open()
c. dump()
d. load()
49. Suresh wants to open the binary file student.dat in read mode. He writes the following statement but he
does not know the mode. Help him to find the same. F=open(‘student.dat’, ____)
a. r
b. rb
c. w
d. wb
50. This method returns an integer that specifies the current position of the file object.
a. seek()
b. load()
c. position()
d. tell()
52. Mr. Zack Sullivan loves programming. He joined an institute for learning. He is learning python. He
learned all the python concepts like strings, lists, tuple, dictionaries etc. but he wants to learn file handling in
python. He is trying to learn binary file handling. His teacher gave him partial code to write and read data
from employee.dat having structure empno, name, salary. Help Zack to complete the code:
___________________ # statement 1
def addrecords():
fw= _____________ #statement 2
dict={} ch=’y’
while ch==’y’:
eno=int(input(“enter employee number”))
nm= input(“enter employee name”)
sal=int(input(“enter employee salary”))
dict={‘empno’:eno,’name’:nm,’salary’:sal}
____________________ # statement 3
ch=input(“add more record”)
fw.close()
2. Which statement is used from the following for statement 2 to open the binary file in write mode?
a. open(“employee.dat”,’w’)
b. open(“employee.dat”,’wb’)
c. open(“employee.dat”,’w+’)
d. open(“employee.dat”,’r’)
3. Which statement is used from the following for statement 3 to write dictionary data created in above code,
namely dict, is written in binary file employee.dat file?
a. pickle.dump(dict,fw)
b. pickle.dump(fw,dict)
c. fw.dump(dict)
d. pickle.store(dict)
4. Which statement is used from the following for statement 4 to open the binary file in read mode?
a. open(“employee.dat”,’r’)
b. open(“employee.dat”,’r+’)
c. open(“employee.dat”,’a’)
d. open(“employee.dat”,’rb’)
5. Complete statement 5 to read data in dictionary namely dict from the opened binary file?
a. dict=pk.read(fr)
b. dict=pickle.load(fr)
c. pickle.load(dict,fr)
d. none of these
53. Now Mr. Zack has given the following code to modify the records of employees from employee.dat used
in above code. He has to increase Rs. 2000 in the salary of those who are getting less than 15000. Mr. Zack
has to find the records and change the salary in place. His teacher gave him partial code. Help him to complete
the code.
import pickle as pk
found=False
emp={}
fin = ___________ #1 statement : open file both in read write mode
# read from file
try:
while true:
pos= _______ #2 store file pointer position before reading record
emp=_______ #3 to read the record in emp dictionary
if emp[‘salary’]<15000:
emp[‘salary’]+=2000
_________ #4 place file pointer at exact location of record
pickle.dump(emp,fin)
found=True
except EOFError:
if found==False:
print(“record not found”)
else:
print(“successfully updated”)
fin.close()
1. In #1 statement open the file in read and write mode. Which statement is used out of the following?
a. open(“employee.dat”,’rb+’)
b. open(“employee.dat”,’r+’)
c. open(“employee.dat”,’a’)
d. open(“employee.dat”,’rb’)
2. Choose the appropriate statement to complete #2 statement to store file pointer position before reading
record.
a. pk.seek(pos)
b. fin.tell()
c. pk.position()
d. pk.tell()
3. Choose the appropriate statement to complete #3 statement to read record in emp dictionary.
a. pk.read(fin)
b. pickle.load(fin,emp)
c. pk.dump(emp)
d. pk.load(fin)
4. Choose the appropriate statement to complete #4 statement to place file pointer at exact location of
record
a. fin.seek(pos)
b. pos=fin.seek()
c. fin.position()
d. none of the above
54. Which of the following file mode opens a file for reading and writing both as well as overwrite the
existing file if the file exists otherwise creates a new file?
a) w
b) wb+
c) wb
d) rwb
55. Which of the following file mode opens a file for append or read a binary file and moves the files pointer
at the end of the file if the file already exist otherwise create a new file?
a) a
b) ab
c) ab+
d) a+
56. Ms. Suman is working on a binary file and wants to write data from a list to a binary file. Consider list
object as l1, binary file suman_list.dat, and file object as f. Which of the following can be the correct
statement for her?
a) f=open(‘sum_list’,’wb’);
pickle.dump(l1,f)
b) f = open(‘sum_list’,’rb’);
l1=pickle.dump(f)
c) f = open(‘sum_list’,’wb’);
pickle.load(l1,f)
d) f = open(‘sum_list’,’rb’);
l1=pickle.load(f)
57. Which option will be correct for reading file for suman from q-56?
58. In which of the file mode existing data will be intact in binary file?
a) ab
b) a
c) w
d) wb
Incomplete Code
import_____ #Statement-1
fh = open(_____, _____, newline='') #Statement-2
stuwriter = csv._____ #Statement-3
data = []
header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION']
data.append(header)
for i in range(5):
roll_no = int(input("Enter Roll Number : "))
name = input("Enter Name : ")
class = input("Enter Class : ")
section = input("Enter Section : ")
rec = [ ____ ] #Statement-4
data.append(rec)
stuwriter. _____ (data) #Statement-5
fh.close()
1. Identify the suitable code for blank space in line marked as Statement-1.
a. csv file
b. CSV
c. csv
d. Csv
2. Identify the missing code for blank space in line marked as Statement-2?
a. "School.csv","w"
b. "Student.csv","w"
c. "Student.csv","r"
d. "School.csv","r"
3. Choose the function name (with argument) that should be used in the blank space of line marked as
Statement-3
a. reader(fh)
b. reader(MyFile)
c. writer(fh)
d. writer(MyFile)
4. Identify the suitable code for blank space in line marked as Statement-4.
a. 'ROLL_NO', 'NAME', 'CLASS', 'SECTION'
b. ROLL_NO, NAME, CLASS, SECTION
c. 'roll_no', 'name', 'Class', 'section'
d. roll_no, name, Class, section
5. Choose the function name that should be used in the blank space of line marked as Statement-5 to create
the desired CSV File?
a. dump()
b. load()
c. writerows()
d. writerow()
70. When you read csv file using csv.reader() function it returns the values in _______ object.
a. dictionary
b. tuple
c. nested list
d. sets
71. CSV module allows to write multiple rows using ____________ function.
a. writerows( )
b. writerow( )
c. writer( )
d. None of the above
72. Which of the following parameter needs to be added with open function to avoid blank row followed
file each row in CSV file?
a. delimiter
b. newline
c. writer, delimiter
d. file object
74. Observe the following code and fill the blank in statement1
import csv
with _________ as f: #statement1
r = csv.______(f) #statement2
for row in ______: #statement3
print(_____) #statement4
a. open("data.csv")
b. f=open("data.csv")
c. Both A & B are Correct
d. Both A & B are incorrect