First Term Text File Qp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Class 12 Computer Science (CS083)

Worksheet 4 (Text Files)


1) ________method is used to read n number of characters of data from a data file using
the file object <file>.
2) Write the file mode/s which generate error if the file doesn't exist.
3) With reference to the given tree structure containing different folders subfolders and
files, mention the path of the file.

i) Give the Absolute path to access the file POEMS.DOC

ii) Give the Relative path to access the file BOOKS.TXT (Working folder is TEXTBOOKS)

4) Explain the differences between Text Files and Binary Files.


5) Explain the different modes of opening a file with its differences.
6) Write a function in Python to read a text file, Alpha.txt and displays those lines which
begin with the word ‘You’.
7) Write a function, vowelCount() in Python that counts and displays the number of
vowels in the text file named Poem.txt..
8) Suppose content of 'Myfile.txt' is
Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
All the king's horses and all the king's men
Couldn't put Humpty together again
What will be the output of the following code?
myfile = open("Myfile.txt")
record = myfile.read().split()
print(len(record))
myfile.close()
a. 24 b. 25 c. 26 d. 27
9) Suppose content of 'Myfile.txt' is
What will be the output of the following code?
myfile = open("Myfile.txt")
line_count = 0
data = myfile.readlines()
for line in data:
if line[0] == 'T':
line_count += 1
print(line_count)
myfile.close()
a) 2 b) 3 c) 4 d) 5
10) Prredict output:
myfile = open(“Myfile.txt")
line_count = 0
data = myfile.readlines()
print(len(data))
myfile.close()
11) Write a method COUNTLINES() in Python to read lines from text file ‘TESTFILE.TXT’
and display the lines which are not starting with any vowel.
Example: If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES() function should display the output as: The number of lines not starting
with any vowel – 1
12) Write a function ETCount() in Python, which should read each character of a text file
“TESTFILE.TXT” and then count and display the count of occurrence of alphabets E and T
individually (including small cases e and t too).
Example: If the file content is as follows:
Today is a pleasant day.
It might rain today.
It is mentioned on weather sites
The ETCount() function should display the output as:
The number of E or e: 6
The number of T or t : 9

You might also like