CHAPTER-5 Textfiles
CHAPTER-5 Textfiles
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