Q1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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

Q2 = What is the significance of file-object?

Q3 = How is file open() function different from close() function ?

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.

Q5 = When a file is opened for output, what happens when


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

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

Q7 = What are the advantages of saving data in:

(i) Binary form


(ii) Text form

Q8 = When do you think text files should be preferred over binary files?

Q9 = Write a statement in Python to perform the following operations:

(a) To open a text file "BOOK.TXT" in read mode.


(b) To open a text file "BOOK.TXT" in write mode.

Q10 = When a file is opened for output, what happens when


(i) The mentioned file does not exist
(ii) The mentioned file does exist
Q11. What role is played by file modes in file operations? Describe the various file mode
constants
and their meanings

Q12. How many file objects would you need to create to manage the following situations?
Explain.

(i) To process three files sequentially.


(ii) To merge two sorted files into a third file.

Q13. Is csv file different from a text file? Why/why not?

Q14. Is csv file different from a binary file? Why/why not?

Q15. Why are csv files popular for data storage?

Q16. How do you change the delimiter of a csv file while writing into it?

Q17. When and why should you suppress the EOL translation in csv file handling?

Q18. If you rename a text file's extension as .csv, will it become a csv file? Why/why not?
ANSWERS
1 ."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.

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

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

c. Format to Open :-
d. file = open ("C:\\Myfiles\\Text1.txt", 'rb')
e. Format to Write :-
f. file = open (r"C:\Myfiles\Text1.txt", 'wb')

2. (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.

3. (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.
• If file is open in append mode then the data in the file is retained and
new data being written will be appended to end.

4. File mode refers to how the file will be used once it's opened.

5. 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.
6. File mode refers to how the file will be used once it's opened.
7. 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.

8. (i)
• It is faster and use less memory.
• Computer can easily understand this form.
• We Can Store Any type of Data in it.

(ii)
• It can easily understand by human.
• It can excess easily.

9. When people want to read the data of the file then text file easy to
understand, binary file not easily understand by human. At that time
text files should be preferred over binary files.

10. (a)
11. File = open ("BOOK.txt", 'r')

12. (b)
13. File = open ("BOOK.txt", 'w')

14. (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.

15. (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.
• If file is open in append mode then the data in the file is retained and
new data being written will be appended to end.
16. File mode refers to how the file will be used once it's opened.

17. 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.

(a) One object are needed.


(b) Three object are needed.

18. Yes, CSV file is different from a text file because CSV file are Comma Separated Value
files and these files take the extension as .csv .While text files are the files which store the
text in the same form as typed. These file have a file extension as .txt .

19. Yes, CSV file is different from a binary file because CSV file are Comma Separated
Value files and easily readable or understand by humans. While binary files easily
understandable by computers
20. By typing these type of syntax :-

<name-of-writer-object> = csv.write (<file-handle>, [delimiter = <delimiter-character>])


21. If you create a csv file on one operating system and use it on another. The EOL of one
operating system will not be treated as EOL on another operating system. Also, if
you have given a character, say '\r', in your text string (not as EOL but as part of a
text string) and if you try to open and read from this file on a Macintosh system,
what would happen? Mac OS will treat every '\r' as EOL - yes, including the one you
gave as part of the text string.

22. So what is the solution? Well, the solution is pretty simple. Just suppress the EOL
translation by specify third argument of open() as newline = (null string - no space
in quotes).
23. If you specify the newline argument while writing onto a csv file, it will create a csv
file with no EOL translation and you will be able to use csv file in normal way on any
platform.

24. Yes, text will become a csv file. Because if data is separated by any delimiter then that
file can be form csv file.

You might also like