Textfilehandling Worksheet2
Textfilehandling Worksheet2
Questions:
S.No. 1Mark Questions
1. What is the extension of regular text files?
a).txt b).dat
c).ppt d).doc
2. Which files can be opened in human readable form?
a) binaryfiles b)textfiles
c)Bothaandb d)None
3. What is the default mode in which text file is opened?
a)write b)read
c)append d)None
4. Whichstatementis correctforopeningthe file?
a) f=open(“c:\\data.txt”,”r”)
b) f=open(r”c:\data.txt”,”r”)
c) Bothaandb d)None
8. Whichofthefollowingfunctionisusedtoreadallthelinesofthetext file?
a)readline() b)read()
c)readlines() d)readit()
a) BothAandRaretrueandRisthecorrectexplanation ofA.
b) BothAand R aretruebutR isnot thecorrect explanation ofA.
c) Aistruebut R isfalse.
d) RistruebutAisfalse.
20. Assertion(A):read()andreadline()areusedtoreadthedatafromthetext file.
Reasoning(R):readlines()functionisusedtoreadalllinesfromthefile in the form of a List.
a) BothAandRaretrueandRisthecorrectexplanation ofA.
b) BothAand R aretruebut R isnot thecorrect explanation ofA.
c) Aistruebut R isfalse.
d) RistruebutAisfalse.
2Mark questions
1. Whatisthedifferencebetweenread() andreadline()functionoftextfiles?
2. What is the difference between readlines() and readline()function used with the text file?
3. Namethefunctionsusedtowritedataonthetextfiles. Explain
4. Whatisthedifferencebetween‘w’and‘a’modeusedwhileopeningatextfile?
5. Whatisthedifferencebetween‘r+’modeand ‘w+’modeusedwhileopeningthetextfile?
6. Ifthefocus.txtfilecontainsthefollowingtext:
Mindfulness,cognitivetraining,andahealthylifestylemayhelpsharpen
yourfocus.
Findtheoutputofthefollowingcode: F=open(“focus.txt”,’r’)
S=F.read(11)
print(S)
F.close()
7. Findtheoutput of thefollowingcode:
F=open(“focus.txt”,’r’)
S= F.readline()
print(S)
T=F.readline()
print(T)
F.close()
8. Findtheoutput of thefollowingcode:
F=open(“focus.txt”,’r’)
L= F.readlines()
forainL:
print(a.upper())
F.close()
9. Findtheoutput of thefollowingcode:
F=open(“focus.txt”,’a+’)
S=“sleepreducesstresshormonesthatcanbeharmfultothebrain”
F.write(S)
F.seek(0)
L=F.readlines()
print(L)
F.close()
10
Findtheoutput of thefollowingcode:
F=open(“wish.txt”,’w’)
F.write(“Day”)
F.close()
Writeaprogramtoreadthefiledata.txtandcountnumberoflinespresentinit
6.
7. Writeaprogram toread thefiledata.txtand displayonlythedigitspresentin it.
8. Writeaprogramtoreadthefilestory.txtanddisplaysecondlastlineofthe file.
9. Writeaprogramtoreadthefilearticle.txtandcountoccurrencesofwords“the”in the file
10 Writeaprogramtoreadthefileletter.txtanddisplaythosewordswhichhasless than or equal to four
characters..
4Marksquestions
1 Writepythonstatementsforopeningthefollowingfiles.Also,writethePython statements to open the
following files:
a) atextfile“example.txt”in bothreadandwritemode
b) atextfile“bfile.dat” inwritemode
c) atextfile“try.txt”inappendandreadmode
d) atextfile“btry.dat”inreadonlymode.
3 Writeamethod/functionCOUNTLINES_ET()inpythontoreadlinesfrom atext
fileREPORT.TXT,andCOUNTthoselineswhicharestartingeitherwith‘E’and starting with
‘T’respectively. Display the Total count separately.
4. Write afunction filter(oldfile, newfile) that copies all the linesofatext file
“source.txt”onto“target.txt”exceptthoselineswhichstartswith“@”sign.
5Marks questions
1 (i) DifferentiatebetweenTextfilesandBinaryfiles.
(ii) Write a method COUNTWORDS() in Python to read data from text file
‘ARTICLE.TXT’anddisplaythecountofwordswhichendswithavowel. For
example, if the file content is as follows:
Anappleadaykeepsyouhealthyand wise
TheCOUNTWORDS()function shoulddisplaytheoutput as:
Totalwordswhichendswithvowel=4 def
COUNTWORDS():
2 (i) Explainseek()andtell()methods.
WriteafunctionCOUNT()inPythontoreadfromatextfile‘rhym.txt'anddisplay the count
of words in each line.
Example:Ifthecontentof‘rhym.txt’isasfollows: Jack and
jill
Wentupthehill To
enjoy
ThentheCOUNT()functionshoulddisplayoutputas:
Line 1 : 3
Line2:4
Line3:2
(ii) WriteafunctionCOUNT()inPythontoreadfromatextfile‘rhym.txt'anddisplay the count
of words in each line.
Example:Ifthecontentof‘rhym.txt’isasfollows: Jack and
jill
Wentupthehill To
enjoy
ThentheCOUNT()functionshoulddisplayoutputas: Line 1
:3
Line2:4
Line3:2
3 (i) Differentiatebetweenw+anda+filemodes.
WriteafunctionWE_WORDS()inPythontoreadfromatextfile‘TEXT.TXT’and display the
count of words which starts with ‘WE’.
Example: If the content of ‘TEXT.TXT’is as follows:
WEMUSTWELCOMEALLWEATHERFROMWEST
ThentheWE_WORDS()functionshould displayoutput as:
TOTALWORDSSTARTINGWITHWE=4
5 (i) Explaintheuseofseek()method.
withreferencetowhichposition,theoffsethastobecounted.Itcanhaveanyofthe following
values:
0 -beginningof thefile
1 -currentpositionofthefile
2 -end offile
Bydefault,thevalueofreference_pointis 0,i.e.theoffsetiscountedfromthe beginning of the
file.
Apre-existingtextfileinfo.txthassometextwritteninit.Writeapythonfunction countvowel()
that reads the contents of the file and counts the occurrence of vowels(A,E,I,O,U) in the
(ii) file.