Professional Documents
Culture Documents
Q1
Q1
Q1
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.
Q6 = What role is played by file modes in file operations? Describe the various file mode
constants and their meanings.
Q8 = When do you think text files should be preferred over binary files?
Q12. How many file objects would you need to create to manage the following situations?
Explain.
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.
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.
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 :-
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.