PyForSchool's Computer Science with Python Class XII 2023-24 [Revision Notes & Question Bank]

An outstanding 4.6 out of 5-star rating on Amazon!

Buy Now

File Handling - Text File

[Set – 1]

1. Write a program that writes 10 random numbers to a file 'numbers.txt'. Each random number should be in the range of 1 through 100. Solution

2. Write a program that reads and display all of the numbers stored in the file numbers.txt (created in question 1) and calculates their total. Solution

3. Write a function, digit_count() in Python that counts and displays the number of digits in the text file named 'sample.txt'. For example, if the content of 'sample.txt' is as follows :

The team achieved a milestone in 2023. They completed a multi-million-dollar project ahead of schedule. Stakeholders were impressed with a 98% success rate.

The function should display the output as 6 Solution

4. Write a function lines_count() that reads lines from a text file named 'zen.txt' and displays the lines that begin with any vowel. Assume the file contains the following text and already exists on the computer's disk:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

The lines_count() function should display the output as:
Explicit is better than implicit.
Solution

5. Assume that the file 'notes.txt' containing some text and exists on the computer’s disk. Write a program that display only those words from 'notes.txt' file whose length is more than seven. Keep in mind that any punctuation marks at the beginning or end of a word should also be considered as part of the word's length. Solution

6. Write a function last_digit_words() in Python to count the words ending with a digit in a text file "notes.txt". For example, if the file content is as follows :

The Computer6 hums softly as I sit with a Book3 in hand, diving into a world of imagination. Outside, my friends gather at House9 and I quickly grab my Pen2 to jot down the address.

The expected output should be:
Number of words ending with a digit are 4 Solution

7. Assume that a file 'names.txt' containing a series of names (as strings) exists on the computer’s disk. Write a function, first_five() that displays only the first five lines of the file’s contents. If the file contains less than five lines, it should display the file’s entire contents. Solution

8. Write a Python program that reads a text file and prints its contents in reverse order (from the last line to the first line). Solution

9. Write the definition of a Python function named long_lines( ) which reads the contents of a text file named 'lines.txt' and displays those lines from the file which have at least 8 words in it. For example, if the content of 'lines.txt' is as follows :

Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.

The output should be:
Special cases aren't special enough to break the rules. Solution

10. Assume that a file named 'feedback.txt' contains student feedback in the following format:

Positive: Saksham improved grades, more confident now.
Negative: Arav needs better time management for coursework.
Negative: Samar should work on communication in group activities.
Negative: Soham could benefit from asking more questions in class.
Positive: Sakshi excels academically, a great team player.

Write a Python function named feedback_analysis() to calculate and display the following information:
Total feedbacks stored in the file.
Count of positive feedbacks.
Count of negative feedbacks. Solution

11. Create a Python function make_copy() that reads a text file 'input.txt' and writes its contents to a new file 'output.txt', capitalizing the first letter of each word. For example, if 'input.txt' contains the following content:

"In the world of programming, there are no limits to what you can achieve. Aim high!"
The 'output.txt' should contain:
"In The World Of Programming, There Are No Limits To What You Can Achieve. Aim High!" Solution