Let’s Master Python Text Files Together
If you’ve started your Python journey, you’ve probably realized that code is great, but data is what makes it useful. Mastering File Handling is that “bridge” where your programs stop being just math and start dealing with real-world information. Whether you’re a Class 12 CBSE student, a BCA/B.Tech grad, or just someone learning to code for fun, knowing how to handle .txt files is a skill you’ll use for the rest of your life.
Why am I calling this an “Important Question” topic?
I’ve spent a lot of time looking at Previous Year Questions (PYQs), and let me tell you—Text Files are a board examiner’s favorite topic. Why? Because they test your logic in three ways: how you read, how you filter, and how you clean up data.
If you can solve these, you’re not just passing an exam; you’re learning how to build real database logic. To make things easy, I’ve compiled 30 practice questions that go from “easy” character counts to “advanced” file merging.
How to get the most out of this guide:
-
Read the question carefully: Make sure you understand exactly what the function needs to do.
-
Write the code yourself first: Don’t be afraid to make mistakes! Trying it on your own is the best way to prepare for your exams.
-
Check the ‘View Solution’ link: Once you’ve tried it, click the link to see my code and a simple, step-by-step explanation of the logic.
Let’s sharpen your Python skills and get you ready to ace those exams!
| 1. Write a function countConsonants() that reads data from story.txt and returns the total number of consonants present in the file. | |
| Answer : Check your logic: Count Consonants Solution | |
| 2. Write a function displaySpecialSymbols() that reads data.txt and displays all special symbols present in the file. | |
| Answer : Check your logic: Display Special Symbol Solution | |
| 3. Write a function sumOfDigits() that reads letter.txt and calculates the sum of all digits found in the file | |
| Answer : Check your logic: Sum of all digit Solution | |
| 4. Write a function replaceDigits() that reads data from secure.txt, replaces every digit with the # symbol, and stores the modified content into system.txt. Example : User123 is my password. O/p => User### is my password. | |
| Answer : Check your logic: Replace Digits Solution | |
| 5. Write a function alphabetFrequency() that reads article.txt and counts the frequency of each alphabet from a to z, ignoring case sensitivity | |
| Answer : Check your logic: Count the Frequency Solution | |
| 6. Write a function displayPalindromicWords() that reads letter.txt and displays all palindromic words present in the file | |
| Answer : Check your logic: Display Palindromic Word Solution | |
| 7. Write a function countSpecialSymbolWords() that reads words.txt and counts how many words start with a special symbol. | |
| Answer : Check your logic: Count Word starting with Special Symbol Solution | |
| 8. Write a function findLongestWord() that reads paragraph.txt and displays the longest word present in the file | |
| Answer : Check you logic : Longest Word Solution | |
| 9. Write a function removeDuplicateWords() that reads content.txt, removes all duplicate words, and writes only unique words back to the same file. Example: red blue red green → red blue green | |
| Answer : Check your logic : Remove Duplicate Words Solution | |
| 10. Write a function displayLongWords() that reads notes.txt and displays all words whose length is greater than 5. | |
| Answer : Check your logic : Display Words where Length Greater than 5. | |
| 11. Write a function countFullStopLines() that reads report.txt and counts the number of lines ending with a full stop (.). | |
| Answer : Check your logic : Count Full Stop Lines Solution | |
| 12. Write a function displayAlternateLines() that reads log.txt and displays only alternate lines (1st, 3rd, 5th, …). | |
| Answer : Check your logic : Display Alternate Lines Solution | |
| 13. Write a function lineWiseWordCount() that reads records.txt and displays the number of words present in each line. | |
| Answer : Check your logic : Count word linewise Solution | |
| 14. Write a function transferErrorLines() that transfers all lines containing the word “error” from server.txt to error_log.txt and removes those lines from server.txt. | |
| Answer : Check your logic : Transfer All Lines Solution | |
| 15. Write a function deleteLinesByCharacter() that deletes all lines from letter.txt that start with a character entered by the user. | |
| Answer : Check your logic : Delete Lines Starting with Specific Character Solution | |
| 16. Write a function countUppercase() that reads message.txt and returns the total number of uppercase letters. | |
| Answer : Check your logic : Count UpperCase Letter Solution | |
| 17. Write a function displayNumericWords() that reads data.txt and displays all words that contain at least one digit. | |
| Answer : Check your logic : Diplay Words that contains at least one digit solution | |
| 18. Write a function countVowelWords() that reads story.txt and counts how many words start with a vowel. | |
| Answer : Check your logic : Count Wods starts with Vowels Solution | |
| 19. Write a function copyEvenLengthWords() that reads words.txt and copies all even-length words into even.txt. | |
| Answer : Check your logic : Copy Even Length Words Solution | |
| 20. Write a function reverseFileContent() that reads note.txt and writes the entire content in reverse order into reverse.txt. | |
| Answer : Check your logic : Reverse Entire File Content Solution | |
| 21. Write a function countBlankLines() that reads paragraph.txt and counts how many blank lines are present. | |
| Answer : Check your logic : Count Blank Line Solution | |
| 22. Write a function displaySmallestWord() that reads text.txt and displays the smallest word (based on length). | |
| Answer : Check your logic : Display Smallest Word Solution | |
| 23. Write a function capitalizeFirstLetter() that reads story.txt and writes each word with its first letter capitalized into newstory.txt. | |
| Answer : Check your logic : Capitalize First Letter Solution | |
| 24. Write a function removeExtraSpaces() that reads data.txt and writes the content into clean.txt after removing all extra spaces between words. | |
| Answer : Check your logic : Remove extra space solution | |
| 25. Write a function countPunctuation() that reads article.txt and counts how many punctuation marks (.,!?:;) are present. | |
| Answer : Check your logic : count punctuation solution | |
| 26. Write a function displayWordsEndingWithDigit() that reads info.txt and displays all words ending with a number. | |
| Answer : Check your logic : Display Word ending with Digit Solution | |
| 27. Write a function sortWords() that reads words.txt, sorts all words alphabetically, and writes them into sorted.txt. | |
| Answer : Check your logic : Sort word solution | |
| 28. Write a function mergeTwoFiles() that merges file1.txt and file2.txt into combined.txt line by line. | |
| Answer : Check your logic : Merger two files solution. | |
| 29. Write a function countLinesWithWord() that reads report.txt and counts how many lines contain the word “success”. | |
| 30. Write a function removeNumericLines() that removes all lines containing any digit from details.txt and stores the remaining lines in final.txt. | |
| Answer : Check your logic : Remove lines containing digits solution. |
Exam Special Recommendation: If you are studying in 12th class and preparing for your board exam, then go for the “Computer Science with Python Sample Paper Book“. It contains 3 previous years’ papers and 7 practice papers solved strictly as per the CBSE pattern. Click here to purchase your copy!