Text file reading writing searching and counting PYQ part 2
What We'll Learn
A quick overview of today's lecture
Text file reading writing searching and counting PYQ part 2
PYQ PRACTICE SCOPE: Text file reading writing searching and counting.
Concept ID: U1_TEXT_FILE_OPERATIONS.
Use only previous-year questions whose concept_ids include U1_TEXT_FILE_OPERATIONS.
Key things we'll cover
Text file reading writing searching and counting
PYQ references used by both prompts9 included0 excluded
Flagged questions are excluded from both Study Notes and PPT references until corrected in Paper Analyzer.
Included
ID 6556 · 2022 · 3 mark(s)
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
File Handling → Text file reading writing searching and counting
Included
ID 6557 · 2022 · 3 mark(s)
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:
E or e: 6
T or t : 9
File Handling → Text file reading writing searching and counting
Included
ID 6301 · 2023 · 1 mark(s)
Assertion(A): xreadlines() reads all the lines from a text file and returns the lines along with newline as a list of strings.
Reasoning (R): readline() can read the entire text file line by line without using any looping statements.
(a) Both (A) and (R) are true and (R) is the correct explanation for (A).
(b) Both (A) and (R) are true and (R) is not the correct explanation for (A).
(c) (A) is true but (R) is false.
(d) (A) is false but (R) is true.
File Handling → Text file reading writing searching and counting
Included
ID 6116 · 2023 · 3 mark(s)
(a) Write the definition of a Python function named LongLines() which reads the contents of a text file named 'LINES.TXT' and displays those lines from the file which have at least 10 words in it.
For example, if the content of 'LINES.TXT' is as follows :
Once upon a time, there was a woodcutter
He lived in a little house in a beautiful, green wood.
One day, he was merrily chopping some wood.
He saw a little girl skipping through the woods, whistling happily.
The girl was followed by a big gray wolf.
Then the function should display output as:
He lived in a little house in a beautiful, green wood.
He saw a little girl skipping through the woods, whistling happily.
File Handling → Text file reading writing searching and counting
Included
ID 6117 · 2023 · 3 mark(s)
(b) Write a function count_Dwords() in Python to count the words ending with a digit in a text file "Details.txt".
Example:
If the file content is as follows :
On seat2 VIP1 will sit and
On seat1 VVIP2 will be sitting
Output will be:
Number of words ending with a digit are 4
File Handling → Text file reading writing searching and counting
Included
ID 6314 · 2023 · 3 mark(s)
(b) Write a function RevString() to read a textfile "Input.txt" and prints the words starting with 'O' in reverse order. The rest of the content is displayed normally.
Example :
If content in the text file is :
UBUNTU IS AN OPEN SOURCE OPERATING SYSTEM
Output will be :
UBUNTU IS AN NEPO SOURCE GNITAREPO SYSTEM
(words 'OPEN' and 'OPERATING' are displayed in reverse order)
File Handling → Text file reading writing searching and counting
Included
ID 6601 · 2023 · 3 mark(s)
Write a function, vowelCount() in Python that counts and displays the number of vowels in the text file named Poem.txt.
File Handling → Text file reading writing searching and counting
Included
ID 6344 · 2024 · 1 mark(s)
Assertion (A) : If numeric data are to be written to a text file, the data needs to be converted into a string before writing to the file.
Reason (R) : write() method takes a string as an argument and writes it to the text file.
(A) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation of Assertion (A).
(B) Both Assertion (A) and Reason (R) are true, but Reason (R) is not the correct explanation of Assertion (A).
(C) Assertion (A) is true, but Reason (R) is false.
(D) Assertion (A) is false, but Reason (R) is true.
File Handling → Text file reading writing searching and counting
Included
ID 6620 · 2024 · 1 mark(s)
Write the missing statement to complete the following code:
file = open("example.txt", "r")
data = file.read(100)
____________________ #Move the file pointer to the beginning of the file
next_data = file.read(50)
file.close()
File Handling → Text file reading writing searching and counting