Text File
Text File
Class: XII Comp. Sci. Department: Computer Science Date of submission: 14/09/2020
1. _____ in Python are interpreted as a sequence or stream or stream of bytes stored on some
storage media.
2. _____ method writes a list of strings to a file.
3. The ________ method of a file object flushes any unwritten information and closes the file
object.
4. The ________ method is used to change the file name or folder name and __________
method is used to remove a file.
5. The read() function reads data from the ___________ of a file.
6. The pickle module produces two main methods _________ and ___________ for writing and
reading operations.
7. The readlines() return a list of lines from the file till __________.
8. The _________ method reads ‘n’ characters from the file.
9. _________ function is used to force transfer of data from buffer to file.
10. The default file open mode is ____________.
11. Opening a file in append mode will place the file pointer at _________ position.
12. A text file stores in __________ or ____________ characters.
13. A ____________ defines the type of operations that is to be performed on the file.
14. ________ function returns a list of strings, each separated by “\n”.
15. ________ statement is used to open a file C:\test.txt for reading.
16. ________ statement is used to read two characters from a file object fobj.
17. ________ statement is used to read the next line of the fie from a file object fobj.
18. ________ statement is used to read the remaining lines of the file from file object fobj.
19. The readlines() method returns ____________.
20. ________ modue is required to use the built-in-function dump().
Section B
1. What is the difference between readline() and readlines() function?
2. Write a single loop to display all the contents of a text file “sample.txt” after removing
leading and trailing whitespaces.
1| 0 4 - 0 9 - 2 0 2 0 / P R E P A R E D B Y : M r . A . R a n j i t h K u m a r
3. Differentiate between fie modes r+ and w+ with respect to Python.
4. Differentiate between file modes r+ and rb+ with respect to Python.
5. What is pickling and unpickling?
Section C
1. What is the output of following code:
F1 = open(“demo.txt”, “r”)
Size = len(F1.read())
print(F1.read(10))
2. Write a statement in Python to open a text file “MyBook.txt” in read mode.
3. Write a statement in Python to open a text file “MyLibrary.txt” in read and write mode.
4. Write a statement in Python to open a text file “MyNote.txt” in write mode.
5. Write a statement in Python to open a text file “MyBook.txt” in append and read mode.
6. Write a statement in Python to open a binary file “Textiles.dat” in read mode.
7. Write a statement in Python to open a binary file “College.dat” in read and write mode
8. Write a statement in Python to open a binary file “Address.dat” in append mode.
Section D
1. Write a function disp_Long() that accepts a file name and display the longest line of the file.
2. Write a method in Python to write multiple lines of text contents into a text file Diary.txt
3. Write a user defined function in Python that displays the number of lines starting with ‘S’ in
the file “Student.txt”
4. Consider a binary file Product.dat containing details such as Prodno:Prodname:Price
(separator (:). Write a Python function to display details of those products with priced more
than 2500.
5. Write a function countWord() in Python to read the text file “Demo.txt” and count the
number of times ‘We’ occurs in the file.
6. Write a method in Python to read lines from a text file “Project.txt” and displays those lines
which start with the alphabet ‘I’.
7. Write a program to display all the records in a fie along with line/record number.
8. Write code to print just the second last line of a text file ‘Test.txt’.
9. Write a function in Python to count the number of lines in a text file ‘Gardent.txt’ which are
starting with the alphabet ‘S’.
10. Write a method DisplayWords() in Python to read lines from a text file BOOK.txt and
display those words which are less than 5 characters.
11. Write a program to display the size of file after removing EOL characters, leading and
trailing white spaces and blank lines.
12. Write a program that copies a text file “Source.txt” onto “Target.txt” barring the lines
starts with “@” sign.
2| 0 4 - 0 9 - 2 0 2 0 / P R E P A R E D B Y : M r . A . R a n j i t h K u m a r