0% found this document useful (0 votes)
26 views2 pages

CHAPTER-5 Textfiles

Doughnuts

Uploaded by

prajaram20675
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)
26 views2 pages

CHAPTER-5 Textfiles

Doughnuts

Uploaded by

prajaram20675
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/ 2

CHAPTER – 5

TEXT FILES
Answer the following questions:
1. Give one difference between Text file and Binary File.
2. Write a Python statement to open a text file “DATA.TXT” so that new contents can be written on it.
3. Write a Python statement to open a text file “DATA.TXT” so that new content can be added to the end of file.
4. Write a Python statement to open a text file “DATA.TXT” so that existing contents can be read from file.
5. A file “MYDATA.TXT” is opened as
file1 = open(“MYDATA.TXT”)
Write a Python statement to close this file.
6. What is the different in file opening mode “a” and “w”?
7. What is the significance of adding „+‟ with file opening mode, with context to „r+‟?
8. What is the difference between readline() and readlines()?
9. What is the purpose of using flush() in file handling operations?
10. What is the advantage of opening file using „with‟ keyword?
11. Considering the content stored in file “trees.txt”
The forest is the town of trees
Where they live quite at their ease,
With their neighbors at their side
Just as we in cities wide.
Write the output of following statements –
f = open("trees.txt")
sr1 = ____________ # to read first line of file
str2 = ___________ # to read next line of file
str3 = ___________ # to read remaining lines of file
12. Considering the content stored in file “habit.txt”
Brush, brush, brush your teeth,
early in the morning!
Take, take, take your bath,
early in the morning!
Comb, comb, comb your hair,
early in the morning!
Drink, drink, drink your milk,
early in the morning!
Go, go, go to school,
early in the morning!

Complete the missing statement using „for‟ loop to print all the lines of file
f = open“habit.txt”)
for ______________________ :
print(____)
13. What is the difference in write() and writelines()?
14. Write a function in python to count the number of lines in “habit.txt” begins from Upper case character.
15. Write a function in python to read lines from file “habit.txt” and count how many times the word “morning” exists
in file.
16. Write a function in python to read lines from file “habit.txt” and display all those words, which has two characters
in it.
For e.g. if the content of file is

Brush,brush,brush your teeth,


early in the morning!
Take, take, take your bath,
early in the morning!
Comb, comb, comb your hair,
early in the morning!
Drink, drink, drink your milk,
early in the morning!
Go, go, go to school,
early in the morning!

Output should be: in in in in Go go go to in


17. Write a function COUNT() in Python to read contents from file “REPEATED.TXT”, to count and display the occurrence
of the word “Catholic” or “mother”.
For example: If the content of the file is “Nory was a Catholic because her mother was a Catholic , and Nory‟s
mother was a Catholic because her father was a Catholic , and her father was a Catholic because his mother was
a Catholic , or had been.
The function should display: Count of Catholic, mother is 9
18. Write a function dispC() in Python to read from text file “habit.txt” and display those lines which starts with “C”.
19. Write a function COUNTSIZE() in Python to read the file “habit.txt” and display size of file. For e.g. if the content of
file is :
20. Write a python function ATOEDISP() for each requirement in Python to read the file “NEWS.TXT” and
(I) Display “E” in place of all the occurrence of “A” in the word COMPUTER.
(II) Display “E” in place of all the occurrence of “A”:
I SELL COMPUTARS. I HAVE A COMPUTAR. I NEED A COMPUTAR. I WANT A COMPUTAR. I USE THAT COMPUTAR.
MY COMPUTAR CRASHED.
The function should display
(I) I SELL COMPUTERS. I HAVE A COMPUTER. I NEED A COMPUTER. I WANT A COMPUTER. I USE THAT COMPTUER. MY
COMPUTER CRASHED.
(II) I SELL COMPUTERS. I HEVE E COMPUTER. I NEED E COMPUTER. I WENT E COMPUTER. I USE THET COMPTUER. MY
COMPUTER CRESHED.
21 . Considering the content stored in file “WORLDCUP.TXT”, write the output
India won the Cricket world cup of 1983
f = open(“WORLDCUP.TXT”)
print(f.read(2))
print(f.read(2))
print(f.read(4))

You might also like