Text File Worksheet
Text File Worksheet
(1 mark questions)
Q1 what is the difference between 'w' and 'a' modes?
Q3 Which of the following functions changes the position of file pointer and returns its new
position?
A. a.)flush()
B. b. tell()
C. c. seek()
D. d. offset()
Q4 which of the following function returns a list datatype
A. d=f.read()
B. d=f.read(10)
C. d=f.readline()
D. d=f.readlines()
Q5 how many file objects would you need to manage the following situations :
(a) to process four files sequentially
(b) To process two sorted files into third file
Q6 The correct syntax of seek() is:
A. file_object.seek(offset [, reference_point])
B. seek(offset [, reference_point])
C. seek(offset, file_object)
D. seek.file_object(offset)
Q7 What will be the output of the following statement in python? (fh is a file handle)
fh.seek(-30,2)
#blank1
f1.close()
(i) what type of file is mydata
(ii) Fill in the blank1 with statement to write "abc" in the file
"mydata" Q5 A given text file data.txt contains :
Line1\n
\n
line3
Line 4
\n
line6
Q6.What would be the output of following code?
f1=open('data.txt')
L=f1.readlines()
print(L[0])
print(L[2])
print(L[5])
print(L[1])
print(L[4])
print(L[3])
Q6 In which of the following file modes the existing data of the file will not be lost?
i) rb
ii) w
iii) a+b
iv) wb
+ v)r+
vi)ab
vii) w+b
viii)wb
ix)w+
Q7 what would be the data types of variables data in following statements?
i) Data=f.read( )
ii) Data=f.read(10)
iii) Data=f.readline()
iv) Data=f.readlines()
Q8 Suppose a file name test1.txt store alphabets in it then what is the output of the following code
f1=open("test1.txt")
size=len(f1.read())
print(f1.read(5))
(3 marks questions)
Q 1 Write a user defined function in python that displays the number of lines starting with 'H' in
the file para.txt
Q2 write a function countmy() in python to read the text file "DATA.TXT" and count the number of
times "my" occurs in the file. For example if the file DATA.TXT contains-"This is my website. I
have diaplayed my preference in the CHOICE section ".-the countmy() function should display
the output as:"my occurs 2 times".
Q3 write a method in python to read lines from a text file DIARY.TXT and display those lines
which start with the alphabets P.
Q4 write a method in python to read lines from a text file MYNOTES.TXT and display those lines
which start with alphabets 'K'
Q5 write a program to display all the records in a file along with line/record number.
Q6 write a program that copies a text file "source.txt" onto "target.txt" barring the lines starting
with @ sign.
Q2 BINARY file is unreadable and open and close through a function only so what are the
advantages of using binary file.