0% found this document useful (0 votes)
73 views6 pages

Textfilehandling Worksheet2

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)
73 views6 pages

Textfilehandling Worksheet2

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/ 6

TEXTFILEHANDLING

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

5. Whichofthefollowingmode cannot be usedforopeningthe text file?


a) ’r’ b)’w+’
c)’a’ d)’rb+’
6. Which iscorrect wayofclosingthe textfile?
a)f.close() b)close(f)
c)Bothaandb d)None
7. Whichstatementiscorrecttoreadnbytesfromtextfileusingfasfile object?
a)f.read(n) b)f.readline(n)
c)Both aandb d)None

8. Whichofthefollowingfunctionisusedtoreadallthelinesofthetext file?
a)readline() b)read()
c)readlines() d)readit()

9. Whatisthereturn datatypeof read() function?


a)string b)List
c)Tuple d)Dictionary
10. Whatisthereturndatatypeofreadlines() function?
a)string b)List
c)Tuple d)Dictionary
11. Whichfunctionis usedto writegroup of charactersonto thetext file?
a)writegroup() b)write()
c)writechar() d)writeall()
12. Which function is used to write List of strings on to the text file? a)writelist()
b)writeline()
c)writelines() d)writeall()
13. Inwhichmodetextfileshouldbeopenedtoaddthedataattheendtothe text file?
a)’r’ b)’w’
c)’a’ d)’b’
14. Whichofthefollowingcommandcanbeusedtoopenthetextfilein writing as well as reading
mode?
a)f=open(“c:\data.txt”,’r’) b)f=open(“c:\data.txt”,’w+’)
c)f=open(c:\\data.txt”,’w+) d)f=open(“c:\\data.txt”,’w’)
15. hichofthefollowingstatementsistrueregardingtheopeningmodesofa file?
a) Whileopeningafileforreading,ifthefiledoesnotexist,anerror occurs.
b) Whileopeningafileforwriting,ifthefiledoesnotexist,anerror occurs.
c) Whileopeningafileforreading,ifthefiledoesnotexist,anewfileis created.
d) Noneof theabove.
16. StateTrueor False
“csvfilesarespecialtext files”
17. StateTrueor False
“textfilesareslowerinprocessingastheyrequirestranslationofspecial characters”
18. Assertion(A):Fileopenedin’w’modealwaysplacesthecursoratthe beginning of the file and
never generates an error.
Reason(R):‘w’modecreatesthefileevenifthefiledoesn’texist.

a) BothAand RaretrueandR isthecorrectexplanation ofA.


b) BothAand R aretruebutR isnot thecorrect explanation ofA.
c) Aistruebut R isfalse.
d) RistruebutAisfalse.
19. Assertion(A):Textfilecontainsdatainhumanreadableform. Reason(R): It executes faster
than any other type of the file.

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

Ifthefilecontains“Good”beforeexecution,whatwillbethecontentsofthefileafter execution of the


above code.
3Marks questions
1. Writeaprogramtoreadtextfilestory.txtandcountnumberoflinesstartingwith letter ‘A’ or ‘a’.
Writeaprogramtoreadthefiledata.txtandcountnumberofuppercase,lowercase in it.
2. Writeaprogramtoreadthefiledata.txtandcountnumberofspacesin it.
3.
4. Writeaprogramtoreadthefilehash.txtanddisplaythenumbercharactersupto first#.
5. Writeaprogramtoreadthefilealphabet.txtanddisplayallthelinesinuppercase

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.

2 (i) Whatisthedifferencebetweenthefollowingsetofstatements(a) and(b):


a) P=open(“practice.txt”,”r”) P.read(10)
b) withopen(“practice.txt”,“r”)asP: x = P.read()
Writeacommand(s)towritethefollowinglinestothetextfilenamedhello.txt. Assume that the file is
opened in append mode.
(ii) “Welcomemyclass” “It is a fun place”
“Youwilllearnandplay”

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.

5 (i) Writeauserdefinedfunctioncountwords()todisplaythetotalnumberofwords present in the


file from a text file “Quotes.Txt”.
Write a function COUNT_AND( ) in Python to read the text file “STORY.TXT”
andcountthenumberoftimes“AND”occursinthefile.(includeAND/and/Andin the
counting)
(ii)

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

4 (i) Whatwillbethereturndatatypeofthefollowingmethods: read() readlines()


Apre-existing text file data.txt has some words written in it. Write a python
functiondisplaywords()thatwillprintallthewordsthatarehavinglengthgreater than 3.
Example:
Forthefiecontent:
Amanalwayswantsto strivehigher inhislifeHewantstobeperfect.
Theoutputafterexecutingdisplayword()willbe:Alwayswantsstrivehigherlife wants perfect

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.

You might also like