0% found this document useful (0 votes)
73 views1 page

Text File Handling Exercises in Python

The document is a worksheet for XII Computer Science students focused on data file handling using Python. It contains a series of programming tasks that require students to read from and manipulate text files, including counting words, reversing lines, and identifying specific characters or patterns. Each question outlines a specific function or method to be implemented, emphasizing practical coding skills in file handling.

Uploaded by

karthikeyan 5137
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 views1 page

Text File Handling Exercises in Python

The document is a worksheet for XII Computer Science students focused on data file handling using Python. It contains a series of programming tasks that require students to read from and manipulate text files, including counting words, reversing lines, and identifying specific characters or patterns. Each question outlines a specific function or method to be implemented, emphasizing practical coding skills in file handling.

Uploaded by

karthikeyan 5137
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

Data File Handling – Worksheet –Text File

XII - Computer Science

[Link] Questions
1 Write a method in Python to read lines from a text file [Link], to find and display the occurrence of
the word 'India'
2 Write a Python function which reads a text file “[Link]” and prints the number of vowels in each line
of that file, separately.
3 Write a method/function BIGLINES() in Python to read lines from a text file [Link], and
display those lines, which are bigger than 20 characters.
4 Write a function COUNTTEXT( ), which reads a text file [Link] and displays all the words of the file
whose length is more than 3 or those which start with ‘A’ or ‘a’ in the form of a list. For example, if the
[Link] file contains India is my country. They are studying. then the output should be: [“India”,
“country”, “They”, “are”, “studying”]
5 Write a Python program that writes the reverse of each line in “[Link]” to another text file “[Link]”.
Eg: if the content of [Link] is: The plates will still shift and the clouds will still spew. The content of
“[Link]” should be: tfihs llits lliw setalp ehT .weps llits lliw sduolc eht dna
6 Write a function named COUNT_CHAR() in python to count and display number of times the arithmetic
operators (+,-,*,/) appears in the file “[Link]” . Example: Solve the following: 1.(A+B)*C/D 2.(A-
B)*(A+B)/D-(E/F) 3. A+B+C/D*(E/F) The function COUNT_CHAR() must display the output as
Number of ‘+’ sign is 4 Number of ‘-‘ sign is 2 Number of ‘*’ sign is 3 Number of ‘/’ sign is 5
7 Write a function V_COUNT() to read each line from the text file and count number of lines begins and
ends with any vowel.
8 Write a user-defined function named Count() that will read the contents of text file named“[Link]”
and count the number of lines which starts with either "I" or "M". E.g. In the following paragraph, there
are 2 lines starting with "I" or "M".
9 Write a function/method DISPLAYWORDS( ) in python to read lines from a text file [Link] and
display those words, which are less than 4 characters.
10 Write a function stats( ) that accepts a filename and reports the file’s longest line.
11 Write a function countdigits() in Python, which should read each character of a text file “[Link]”,
count the number of digits and display the file content and the number of digits. Example: If the
“[Link]” contents are as follows: Harikaran:40,Atheeswaran:35,Dahrshini:30,Jahnavi:48 The output
of the function should be: ('Total number of digits in the file:', 8)
12 Write a function in Python to count the number of lines in a text file ‘[Link]’ which is starting
with an alphabet ‘A’.
13 Write a method SHOWLINES() in Python to read lines from text file [Link]‘ and display the
lines which do not contain 'ke'.
14 Write a function RainCount() in Python, which should read the content ofa text file ―[Link]‖
and then count and display the count of occurrence of word RAIN (case insensitive) in the file. Example:
If the file content is as follows: It rained yesterday It might rain today I wish it rains tomorrow too I love
Rain The RainCount() function should display the output as: Rain – 2
15 Define a function SHOWWORD () in python to read lines from a text file [Link], and display
those words, whose length is less than 5.
16 Write a user defined function in python that displays the number of lines starting with 'H' in the
[Link]
17 Write a function in Python to print those words which contains letter ‘S’ or ‘s’ anywhere in the word in
text file “[Link]”
Write a function COUNTLINES( ) which reads a text file [Link] and then count and display the
18 number of the lines which starts and ends with same letter irrespective of its case

Common questions

Powered by AI

To filter lines that do not contain a particular character combination in a text file, you need to read each line, check for the absence of the sequence using the 'not in' logical construct, and display those lines that do not contain the specified substring. Implementing this involves opening "TESTFILE.TXT", iterating over each line, and using conditional statements to select lines not containing 'ke', then displaying these lines .

To reverse each line's content from a text file and save it to another, you would read the original file line-by-line, reverse the string for each line, and write the reversed lines to the new file. This method involves opening "input.txt" for reading and "output.txt" for writing, using a loop to read each line from "input.txt", applying Python's string slicing feature to reverse the line, and writing the reversed line to "output.txt" .

Counting occurrences of arithmetic operators in a text file involves reading the file character by character while maintaining a count for each of the operators ('+', '-', '*', '/'). Open "Math.txt", iterate over each character in the file, and update respective counters based on character identification. Finally, report the counts for each operator by printing them .

Creating a function to list words below a certain length involves reading lines from the file, splitting them into words, and filtering words based on the length condition. For example, reading from "STORY.TXT", use a loop to check each word's length against the specified value (such as less than 5 characters) and collect these words in a list for display. This process efficiently filters words without modifying the original file .

To count lines that start and end with a vowel, read the text file line-by-line and apply two checks for each line: ensure that the first character is a vowel and the last character (considering \\n stripped) is also a vowel. Use a set for vowels to ensure case insensitivity. This function involves opening the file, iterating through each line, stripping whitespace from both ends of the line, checking if both the initial and final characters after boundaries adjustment are vowels, and updating the count accordingly .

To find the longest line in a text file, read the file line-by-line and compare the lengths of lines to maintain the line with the maximum length. This requires opening the file, initializing a variable to store the maximum line, and iterating through each line to update this variable if a longer line is found. Finally, the length and content of this longest line should be displayed .

To report lines starting with a specific letter in a text file, a Python function should read the file line-by-line and check the first character of each line. Opening the file, iterating through each line, and using a conditional statement to check if the line starts with the designated letter or its case variant, such as 'I' or 'M', and printing such lines would accomplish this. This approach can be used for "STORY.TXT" to count lines beginning with 'A' .

A function to display lines exceeding a specified length can be implemented by reading the file line-by-line, checking the length of each line, and printing those that meet the criteria. For example, reading "CONTENT.TXT" and displaying lines longer than 20 characters requires using a loop to open and iterate through each line of the file, using an if condition to check if the line's length exceeds 20 characters, and then printing these lines .

To count words beginning with a specified letter, read the file, split each line into words, and use a conditional check to see if the first character of each word matches the criteria. Open the target file, split lines into words, and for each word, verify if it starts with the designated letter, counting these instances. The function should then output the count .

A Python function to count vowels in each line of a text file involves reading the file line-by-line, iterating over each character of the line, and checking if it is a vowel (using a set of vowels {'a', 'e', 'i', 'o', 'u', etc., both lowercase and uppercase}). The count for each line should be maintained separately. This function would open "poem.txt", use a loop to iterate through each line, initialize a counter for vowels at the start of each line, and then update the counter for any vowel found, finally printing the count per line .

You might also like