Worksheet 6

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

GD GOENKA SIGNATURE SCHOOL

WORKSHEET V – FILE HANDLING


CLASS XII – COMPUTER SCIENCE
1. Differentiate between:
a) text file and binary file
b) readline() and readlines()
c) write() and writelines()

2. Write the use and syntax for the following methods:

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”

7. Define pickling in Python. Explain serialization and deserialization of Python object.

8. Write a program to enter the following records in a binary file:


Item No integer
Item_Name string
Qty integer
Price float
Number of records to be entered should be accepted from the user. Read the file to display the records in
the following format:
Item No:
Item Name :
Quantity:
Price per item:
Amount: ( to be calculated as Price * Qty)
9. Rohit, a student of class 12th, is learning CSV File Module in Python. During examination, he
has been assigned an incomplete python code (shown below) to create a CSV File 'Student.csv'
(content shown below). Help him in completing the code which creates the desired CSV File.
CSV File
1,AKSHAY,XII,A
2,ABHISHEK,XII,A
3,ARVIND,XII,A
4,RAVI,XII,A
5,ASHISH,XII,A
Incomplete Code
import_____ #Statement-1
fh = open(_____, _____, newline='') #Statement-2
stuwriter = csv._____ #Statement-3
data = []
header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION']
data.append(header)
for i in range(5):
roll_no = int(input("Enter Roll Number : "))
name = input("Enter Name : ")
Class = input("Enter Class : ")
section = input("Enter Section : ")
rec = [_____] #Statement-4
data.append(rec)
stuwriter. _____ (data) #Statement-5
fh.close()

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()

10. Differentiate between Text file and Binary files?

11. Differentiate between file modes r+ and w+ with respect to Python.

12. Write a statement in Python to perform the following operations

A. To open a binary file “LOG.DAT” in read mode


B. To open a binary file“LOG.DAT” in write mode

14. Differentiate between Absolute Pathnames and Relative Pathnames

15. Write a program to display all the lines in a file “python.txt” along with line/record number.

You might also like