Chapter 4 Data File Handling

Q1. What is the difference between "w" and "a" modes?

Answer:
"w" mode :- If file exists, python will delete all data of the file.

"a" mode :- If file exists, the data in the file is retained and new data being written will be appended to end.

Q2. What is the significance of file-object?

Answer:
All functions that we perform on a data file are performed through file-objects.

Q3. How are open() functions different from close() functions other than their functionality of opening and closing files?

Answer:
open () function is a built in function while close () function is a method used with file handle object.

Q4. Write statements to open a binary file C:\Myfiles\Text1.txt in read and write mode by specifying the file path in two different formats.

Answer:
1st Format:
file = open ("C:\\Myfiles\\Text1.txt", 'r')

2nd Format:
file = open (r"C:\Myfiles\Text1.txt", 'w')


Q5. In which of the following file modes will the existing data of the file not be lost?

(a) 'rb'
(b) ab
(c) w
(d) w + b
(e) 'a+b'
(f) wb
(g) wb+
(h) w+
(i) r+

Answer:
In (a), (b), (e), (i) file modes, the existing data of the file do not lost.

Q6. What would be the data type of variable data in the following statements?

(a) data = f.read()
(b) data = f.read(10)
(c) data = f.readline()
(d) data = f.readlines()

Answer:
(a) String type.
(b) String type.
(c) String type.
(d) List type.


Q7. When a file is opened for output, what happens when?

(i)The mentioned file does not exist
(ii)The mentioned file does exist

Answer:
(i)
• If file is open in read mode then it give error.
• If file is open in write or append mode then file created by python.

(ii)
• If file is open in read mode then python read the data according to program.
• If file is open in write mode then python delete all data and write new data according to user.

Q8. What role is played by file modes in file operations? Describe the various file mode constants and their meanings.

Answer:
File mode refers to how the file will be used once it's opened.
There are three types of file modes:-
(1)read mode:It will read data of file according to program.
(2)write mode:Python delete all data and write new data according to user.
(3)append mode:The data in the file is retained and new data being written will be appended to end.


Join our WhatsApp and Telegram group for all the upcoming contents and updates.

1. WhatsApp 

2. Telegram