File Handling in Python
Class 12 · Computer Science · 10 Questions
For a newly created file, is there any difference between write() and append() methods?
Differentiate between: a) text file and binary file b) readline() and readlines() c) write() and writelines()
Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files: a) a text file “example.txt” in both read and write mode b) a binary file “bfile.dat” in write mode c) a text file “try.txt” in append and read mode d) a binary file “btry.dat” in read only mode.
Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it? Will some error message be flashed?
What is the difference between the following set of statements (a) and (b): a) P = open(“practice.txt”,”r”) P.read(10) b) with open(“practice.txt”,“r”) as P: x = P.read()
Write a command(s) to write the following lines to the text file named hello.txt. Assume that the file is opened in append mode. “Welcome my class” “It is a fun place” “You will learn and play”
Write a Python program to open the file hello.txt used in question no 6 in read mode to display its contents. What will be the difference if the file was opened in write mode instead of append mode?
Write a program to accept string/sentences from the user till the user enters “END” to. Save the data in a text file and then display only those sentences which begin with an uppercase alphabet.
Define pickling in Python. Explain serialization and deserialization of Python object.
Write a program to enter the following records in a binary file: Item No: integer Item_Name: string Qty: integer Price: float Number of records to be entered should be accepted from the user. Read the file to display the records in the following format: Item No: Item Name: Quantity: Price per item: Amount: (to be calculated as Price * Qty)