Text File Handling Board Questions


  1. Consider the following Python statement : [1 MARK] [2024]
    F = open ('CONTENT.TXT')

    Which of the following is an invalid statement in Python ?

    (a) F.seek(1,0)
    (b) F.seek(0,1)
    (c) F.seek(0,-1)
    (d) F.seek(0,2)
    
  2. (c) F.seek(0,-1)

  3. What is the advantage of using with clause while opening a data file in python? Also give syntax of with clause. [2 MARKS] [2024]
  4. COMING SOON

  5. Differentiate between ‘w’ and ‘a’ file modes in python. [2 MARKS] [2024]
  6. COMING SOON

  7. Write a user defined function in python named showInLines() which reads contents of a text file named STORY.TXT and displays every sentence in a separate line. Assume that a sentence ends with a full stop (.), question mark (?) or an exclamation mark (!). [3 MARKS] [2024]

    For example
    if a content of file story.txt is as follows:
    Our parents told us that we must eat vegetables to be healthy. And it turns out, our parents were right! So, what else did our parents tell?

    Then the function should display the files content as follows:
    Our parents told us that we must eat vegetables to be healthy.
    And it turns out, our parents were right!
    So, what else did our parents tell?
  8. COMING SOON

  9. Write a function, c_words() in python that separately counts and displays the number of uppercase and lowercase alphabets in a text file Words.txt. [3 MARKS] [2024]
  10. COMING SOON

  11. The syntax of seek( ) is : 
    file_object.seek(offset[, reference_point])
    What is the default value of reference_point ? [1 Mark] [2023]
    
    (a) 0
    (b) 1
    (c) 2
    (d) 3
    
  12. (a) 0

  13. Which of the following mode keeps the file offset position at end of the file? [1 Mark] [2023]
    
    (a) r+
    (b) r
    (c) w
    (d) a
    
  14. (d) a

  15. Write a function count_Dwords( ) in Python to count the words ending with a digit in a text file "Details.txt" . [3 Marks] [2023]
    
    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
    
  16. def count_Dwords():
        f=open("demo.txt","r") 
        s=f.read()
        L=s.split()
        count=0
        for i in L:
            if i[-1] in '0123456789':
                count=count+1
        print("Number of words ending with a digit are",count)
        f.close()
    
    count_Dwords()     
    

  17. Write the definition of 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. [3 Marks] [2023]
    
    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.
    
  18. def LongLines():
        f=open("Lines.txt","r") 
        L=f.readlines()             
        for i in L:
            x=i.split()                 
            if len(x)>=10:
                print(i,end='')
        f.close()
    
    LongLines()