Worksheet 6
Worksheet 6
Worksheet 6
a) open(]
b) read()
c) seek()
d) dump()
3. 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.
4. 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?
5. 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()
6. 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”
I. Identify the suitable code for blank space in line marked as Statement-1.
a) csv file b) CSV c) csv d) Csv
II. Identify the missing code for blank space in line marked as Statement-2?
a) "School.csv","w" b) "Student.csv","w" c) "Student.csv","r" d) "School.csv","r"
III. Choose the function name (with argument) that should be used in the blank space of line
marked as Statement-3
a) reader(fh) b) reader(MyFile) c) writer(fh) d) writer(MyFile)
IV. Choose the function name that should be used in the blank space of line marked as
Statement-5 to create the desired CSV File?
a) dump() b) load() c) writerows() d) writerow()
15. Write a program to display all the lines in a file “python.txt” along with line/record number.