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

Text File Worksheet

Uploaded by

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

Text File Worksheet

Uploaded by

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

QUESTIONS

(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)

Options:- It will place the file pointer:-


A. at 30th byte ahead of current current file pointer position
B. at 30 bytes behind from end-of file
C. at 30th byte from the beginning of the file
D. at 5 bytes behind from end-of file .
Q8 Which Python function is used to open a text file for reading?

A. open("file.txt", "w") B. open("file.txt", "r")


C. read("file.txt") D. write("file.txt")
Q 9 text file student.txt is stored in the storage device. Identify the correct option out of the
following options to open the file in read mode.
i. myfile = open('student.txt','rb')
ii. myfile = open('student.txt','w')
iii. myfile = open('student.txt','r')
iv. myfile = open('student.txt')
A. a. only i
B. b. both i and iv
C. c. both iii and iv
D. d. both i and iii
Q 10 Raman wants to open the file abc.txt for writing the content stored in a folder name sample
of his computer d drive help raman to tell the correct code for opening the file
A. myfile=open(“d:\\sample.txt\\abc”,’w’)
B. myfile=open(“d:\\sample\\abc.txt”,’w’)
C. myfile=open(“d:\\sample\abc.txt”,’w’)
D. all of the above
(2 mark questions)
Q1 write a single loop to display all the contents of a text file file1.txt after removing leading and
trailing WHITESPACES
Q2 what is the output of the following code fragment? explain
out=open('output.txt','w')
out.write('hello,world!\n')
out.write('how are you')
out.close()
open('output.txt').read()
Q3 read the code given below and answer the questions
f1=open('main.txt','w')
f1.write('bye')
f1.close()
if the file contains 'GOOD' before execution, what will be the content of the file after execution of
the code
Q4 observe the following code and answer the follow
f1=open("mydata","a")

#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.

You might also like