Chapter 5 – File Handling in Python
Type A: Short Answer Questions / Conceptual Questions
Question – 1: Difference between “w” and “a” modes
Answer: “w” – Write mode, file open in write mode, and put the cursor at the beginning of the file. If the file is existing then it erases all previous contents.
“a” – Append mode, file open in append mode and put the cursor at the end of the file. if the file is existing then it does not erase the previous contents. Append mode is used to add more content at the end of the file.
Question – 2: Significance of file object
Answer: The File Object serves as a link to a file residing on the computer. Python’s open() function creates a file object. It is also known as file handle.
Question – 3: Difference between open() and close()
Answer: Python’s open() function is used to open a file in reading, write or append mode. It creates and returns a file object. It requires two arguments – file name and file mode. It creates a link between file object and the file on the computer.
file-object = open(filename, file-mode)
Python’s close() function is used to close the file. It terminates the link between the file object and the file on a computer.
Question – 4: Statement to open a binary file in read and write mode in different file path format.
Answer: file-object = open(“c:\\Myfiles\\Text1.txt”, “rb”)
file-object = open(“c:\\Myfiles\\Text1.txt”, “wb”)
file-object = open(r”c:\Myfiles\Text1.txt”, “rb”)
file-object = open(r”c:\Myfiles\Text1.txt”, “wb”)
Question – 5: File Opened in output write mode, then if
Answer: (i) file does not exist, then it creates a blank new file.
(ii) file exists, then it erases all contents and creates a blank new file with the same name.
Question – 6: Role played by file modes. Types of file modes?
Answer: File mode specifies the types of operations i.e. read or write or append, possible in the opened file. File mode refers to how the file will be used once it’s opened.
Types of File Modes are –
|‘r’||Read Only, It will open an existing file for reading only.|
|‘w’||Write only. It will create a file, if the file does not exist, otherwise truncate existing data and overwrite the file.|
|‘a’||Append, If the file does not exist, then create a new file. Otherwise, open the file in appending mode i.e. old data has remained in the file and new content will be appended to the end. Only writing is allowed.|
|r+||Read and Write both operations allowed. The file must exist otherwise an error raise.|
|w+||Write and Read, Open for both Write and Read. Rest same as ‘w’|
|‘a+’||Write and Read both operations allowed. File created if not exist. Otherwise retained the old content and append the new content.|
|rb, rb+, r+b||Open a file for reading. The file must be open, otherwise, an Exception raised. ‘r+b’ or ‘rb+’ means reading and writing both. ‘rb’ for reading only.|
|wb, wb+, w+b||Open a file for writing. If the file does not exist then create another overwrite. ‘w+b’ or ‘wb+’ means writing & reading both. ‘wb’ for writing only.|
|ab, ab+, a+b||Open a file for appending. If the file does not exist then create another open for append. ‘a+b’ or ‘ab+’ means writing & reading both. ‘ab’ for writing only.|
Question – 7: Advantages of saving data in (i) Binary form (ii) Text form (iii) CSV form?
Answer: (i) Binary form –