0% found this document useful (0 votes)
712 views7 pages

Worksheet 2 File Handling

The document discusses binary files and provides examples of using Python's pickle module to write structured data to a binary file and then read it back. It includes a problem to create a binary file with an employee record structure and functions to add records to the file and display details

Uploaded by

Hills High
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
712 views7 pages

Worksheet 2 File Handling

The document discusses binary files and provides examples of using Python's pickle module to write structured data to a binary file and then read it back. It includes a problem to create a binary file with an employee record structure and functions to add records to the file and display details

Uploaded by

Hills High
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Class 12 (CS) Ch – File Handling Revision

1. What does the acronym CSV stand for in its full form?
a. Common Separated Value
b. Comma System Value
c. Comma Separated Value
d. Common System Vault
Ans: c. Comma Separated Value

2. What is the default delimiter of a CSV file


a. Tab
b. Comma
c. Semicolon
d. Space

Ans: b. Comma

3. In regards to separated value files such as .csv and .tsv, what is the delimiter?
a. Any character such as the comma (,) or tab (\t) that is used to separate
the row data
b. Anywhere the comma (,) character is used in the file
c. Delimiters are not used in separated value files
d. Any character such as the comma (,) or tab (\t) that is used to separate
the column data.

Ans: d. Any character such as the comma (,) or tab (\t) that is used to separate
the column data.

4. In separated value files such as .csv and .tsv, what does the first row in the file
typically contain?
a. The source of the data
b. The author of the table data
c. Notes about the table data
d. The column names of the data

Ans: d. The column names of the data

5. Assume you have a file object my_data which has properly opened a separated
value file that uses the tab character (\t) as the delimiter.
What is the proper way to open the file using the Python csv module and assign it
to the variable csv_reader?
Assume that csv has already been imported.
a. csv_reader = csv.tab_reader(my_data)
b. csv_reader = csv.reader(my_data)
c. csv_reader = csv.reader(my_data, tab_delimited=True)
d. csv_reader = csv.reader(my_data, delimiter='\t')

Ans: d. csv_reader = csv.reader(my_data, delimiter='\t')

6. When iterating over an object returned from csv.reader(), what is returned with
each iteration?
For example, given the following code block that assumes csv_reader is an
object
returned from csv.reader(), what would be printed to the console with each
iteration?
for item in csv_reader:
print(item)

a. The individual value data that is separated by the delimiter


b. The row data as a list
c. The column data as a list
d. The full line of the file as a string

Ans: b. The row data as a list.

1. Write a program in python to write and read structure, dictionary to the binary
file.

import pickle
d1={'jan':31,'feb':28,'march':31,'april':30}
f=open('binfile.dat','wb+')
pickle.dump(d1,f)
d2=pickle.load(f)
print(d2)
f.close()

2. BINARY file is unreadable and open and close through a function only so what are
the advantages of using binary file

Binary file are easier and faster than text file. Binary files are also used
to store binary data such as images, video files, and audio files.

3. Write a statement to open a binary file name sample.dat in read mode and the file
sample.dat is placed in a folder ( name school) existing in c drive

f1=open(“c:\school\sample.dat”,’r’)

4. When do you think text files should be preferred over binary files?

Text file should be preferred when we have to save data in text format andsecurity of
file is not important

Case study Question-

Neha is making software on “Items & their prices” in which various records are to be
stored/retrieved in STORE.CSV data file. It consists some records (Item & Price).
She has written the following code in python. As a programmer, you have to help her
to successfully execute the program.
import # Statement-1
def AddItem(Item, Price) # Statement-2
f=open(“STORE.CSV”, ) # Statement-3
fw=csv.writer(f)
fw.writerow([Item, Price])
# Statement-4

def ShowRecord():
with open(“STORE.CSV”,”r”) as NI:
NewItem=csv. (NI) # Statement-5
for rec in NewItem:
print(rec[0], “#”, rec[1])

#main-code
AddItem(“Sugar”, 38.00)
AddItem(“Rice”, 48.50)
ShowRecord() # Statement-6

Q1. Which module should be imported in Statement-1.


A. pickle B. csv C. file D. text

Ans: B. csv

Q2. Which file mode to be passed to add new record in Statement-3.


A. w+ B. w C. wb D. a

Ans: D. a

Q3. What should be written in Statement-4 to close the file?


A. close() B. fw.close() C. f.close() D. csv.close()

Ans: C. f.close()

Q4. Which function to be used in Statement-5 to read the data from a csv
file.
A. read() B. readline() C. readlines() D.

reader()Ans: D. reader()

Q5. Output after executing Statement-6 will be -

A. (“Sugar”, “38.0”) B. Sugar 38.0


(“Rice”, “48.50”) Rice 48.0

C. Sugar, 38.0 D. Sugar # 38.0


Rice, 48.50 Rice # 48.50
Ans: D. Sugar # 38.0
Rice # 48.50

23 Ranjan Kumar of class 12 is writing a program to create a CSV file “user.csv” which will
contain user name and password for some entries. He has written the following code. As a
programmer, help him to successfully execute the given task.

import _____________
csv # Line 1

def addCsvFile(UserName,PassWord): # to write / add data into the


newFileWriter = csv.writer(f)
newFileWriter.writerow([UserName,PassWord])
f.close()

#csv file reading code


def readCsvFile(): # to read data from CSV file
with open(' user.csv','r') as newFile:
newFileReader = csv._________(newFile)
reader # Line 3
for row in newFileReader:
print (row[0],row[1])
newFile.______________ # Line 4
addCsvFile(“Arjun”,”123@456”)
addCsvFile(“Arunima”,”aru@nima”) # Line 5

(a) Name the module he should import in Line 1. csv

(b) In which mode, Ranjan should open the file to add data into the file a

(c) Fill in the blank in Line 3 to read the data from a csv file. reader

(d) Fill in the blank in Line 4 to close the file. close

(e) Write the output he will obtain while executing Line 5.


A binary file “emp.dat” has structure [EID, Ename, designation, salary].
i. Write a user defined function CreateEmp() to input data for a record and create a file
emp.dat.
ii. Write a function display() in Python to display the detail of all employees whose salary is
more than 50000.

import pickle
def CreateEmp():
f1=open("C:\\xii_ip\\emp.dat",'wb')
eid=input("Enter E. Id")
ename=input("Enter Name")
designation=input("Enter Designation")
salary=int(input("Enter Salary"))
l=[eid,ename,designation,salary]
pickle.dump(l,f1)
f1.close()
import pickle
def display():
f2=open("C:\\xii_ip\\emp.dat","rb")
try:
while True:
rec=pickle.load(f2)
if rec[3]>5000:
print(rec[0],rec[1],rec[2],rec[3])
except:
f2.close()
display()

i. A binary file “emp.DAT” has structure (EID, Ename, designation,salary). Write a function to
add more records of employes in existing file emp.dat.

import pickle
def createemp:
f1=open("emp.dat",'ab')
eid=input("Enter E. Id")
ename=input("Enter Name")
designation=input("Enter Designation")
salary=int(input("Enter Salary"))
l=[eid,ename,designation,salary]
pickle.dump(l,f1)
f1.close()

ii. Write a function Show() in Python that would read detail of employee from file “emp.dat”
and display the details of those employee whose designation is “Salesman”.

def display():
f2=open("emp.dat","rb")
try:
while True:
rec=pickle.load(f2)
if (rec[2]=='Manager'):
print(rec[0],rec[1],
rec[2],rec[3])
except:
break
f2.close()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy