Text File Handling Practice Questions


  1. Write a function, vowelCount() in Python that counts and displays the number of vowels in the text file named Poem.txt.
  2. def vowelCount():
          f = open("Poem.txt", "r")
          count = 0
          s = f.read( )
          for i in s:
                if i in 'AEIOUaeiou':
                      count = count + 1
          print("Number of vowels = ", count)
          f.close()
    
    vowelCount()
    

  3. Write a function in Python to read a text file, Alpha.txt and displays those lines which begin with the word 'You'.
  4. def youLines():
          f = open("Alpha.txt", "r")
          count = 0
          L = f.readlines()   #[ "line1", "line2", .......]
          for line in L:
                words = line.split()  #["word1", "word2", .......]
                if words[0] in ["you", "You", "YOU"]:
                      print(line)
          f.close()
    
    youLines()
    

  5. 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
  6. def ETCount():
          f = open("Testfile.txt", "r")
          count_E = 0
          count_T = 0
          s = f.read()
          for i in s:
                if i == 'E' or i == 'e':
                      count_E +=1
                elif i == 'T' or i == 't':
                      count_T +=1
          print("E or e : ", count_E)
          print("T or t : ", count_T)
          f.close()
    
    ETCount()
    

  7. 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
  8. def COUNTLINES():
          f = open("TESTFILE.txt", "r")
          count = 0
          L = f.readlines()   #['line1', 'line2', ......]
          for line in L:
                if line[0] not in "AEIOUaeiou":
                      count += 1
          print("Number of lines not starting with any vowel - ", count)
          f.close()
    
    COUNTLINES()
    

  9. Write a function in Python that counts the number of “Me” or “My” words present in a text file “STORY.TXT”.
    
    Example : 
    If the “STORY.TXT” contents are as follows:
    My first book was Me and My Family. 
    It gave me chance to be Known to the world.
    
    The output of the function should be:
    Count of Me/My in file: 4
  10. def countMeMy():
          f = open("STORY.txt", "r")
          count = 0
          s=f.read()
          wordlist = s.split()    #[ 'word1', 'word2', ......]
          for word in wordlist:
                if word in [ 'Me', 'me', 'ME', 'mE', 'My', 'my', 'MY', 'mY' ]:
                      count = count + 1
          print("Count of Me/My in file : ", count)
          f.close()
    
    countMeMy()
    

  11. Write a method/function DISPLAYWORDS() in python to read lines from a text file STORY.TXT, and display those words, which are less than 4 characters.
  12. def DISPLAYWORDS():
          f = open("Story.txt", "r")
          s=f.read()
          wordlist = s.split()     #['word1', 'word2', ....]
          for word in wordlist:
                if len(word) < 4:
                      print(word)
          f.close()
    
    DISPLAYWORDS()
    

  13. Write a function in python to count the number of lines in a text file ‘STORY.TXT’ which is starting with an alphabet ‘A’ .
  14. def A_lines():
          f = open("story.txt", "r")
          count = 0
          lineslist = f.readlines()    #['line1', 'line2', ....]
          for line in lineslist:
                if line[0] == 'A' or line[0] == 'a' :
                      count = count + 1
          print("No. of lines starting with A = ", count)
          f.close()
    
    A_lines()