File Handle Worksheet
File Handle Worksheet
Q5. Differentiate between file modes r+ and rb+ with respect to Python.
Q6. What would be the data type of variable data in the following statements?
Q11. Write a method in Python to read lines from a text file MYNOTES.TXT and display those
lines which start with the alphabet 'K'.
Q12. Write a function file_long() that accepts a filename and reports the file's longest line.
Q13. Write a program to count the number of uppercase alphabets present in a text file
"Pome.txt".
Q14. Write a function remove_lowercase() that accepts two file names, and copies all lines that
do not start with a lowercase letter from the first file to the second file.
Q15. Write a program that reads characters from the keyboard one by one. All lower case
characters get stored inside the file LOWER, all upper case characters get stored inside the file
UPPER and all other characters get stored inside file OTHERS.
Q16. Write a statement in Python to perform the following operations:
Q18. Write a Python program to display the size of a file after removing EOL characters, leading
and trailing white spaces and blank lines.
Q19. Write a user-defined function in Python that displays the number of lines starting with 'H' in
the file Para.
Q22. 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.
Q23. Write a method in Python to read lines from a text file DIARY.TXT and display those lines
which start with the alphabet 'P'.
Autumn Song
Sarojini Naidu
(b) Modify the program so that the lines are printed in reverse order.
(c) Modify the code so as to output to another file instead of the screen. Let your script overwrite
the output file.
(d)Change the script of part (c) that it append output to an existing file.
(e) Modify the program so that each line is printed with a line number at beginning.
Q25. Write a program using Dictionary and Text files to store roman numbers and find their
equivalents.
Q26. Observe the following code and answer the questions that follow:
Q27. How many file objects would you need to manage the following situations:
Q28. Write code to print just the last line of a text file 'data.txt'.
Q29. Write a function in Python to count the number of lines in a text file 'STORY.TXT' which are
starting with the alphabet 'A'.
Q30. A text file contains alphanumeric text (say an.txt). Write a program that reads this text file
and prints only the numbers or digits from the file.
Q31. Following is the structure of each record in a data file named "PRODUCT.DAT".
The values for prod_code and prod_desc are strings and the value for stock is an integer.
Write a function in Python to update the file with a new value of stock. The stock and the
product_code, whose stock is to be updated, are to be inputted during the execution of the
function.
Q32. Create a CSV file "Groceries" to store information of different items existing in a shop. The
information is to be stored w.r.t. each item code, name, price, qty. Write a program to accept the
data from user and store it permanently in CSV file.