0% found this document useful (0 votes)
3 views

CS final project

The document is a project report on file handling with binary files, completed by Raushan Kumar Singh under the guidance of Mrs. Divya Jyoti. It covers topics such as creating, opening, and closing binary files, serialization using the pickle module, and includes sample Python programs for reading and writing binary files. The report also contains a bibliography of resources used for the project.
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)
3 views

CS final project

The document is a project report on file handling with binary files, completed by Raushan Kumar Singh under the guidance of Mrs. Divya Jyoti. It covers topics such as creating, opening, and closing binary files, serialization using the pickle module, and includes sample Python programs for reading and writing binary files. The report also contains a bibliography of resources used for the project.
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/ 15

COMPUTER SCIENCE

PROJECT FILE

TOPIC: FILE HANDLING (BINARY


FILES)
CERTIFICATE

This is to certify that Raushan Kumar Singh of class


XII of
SLS DAV PUBLIC SCHOOL, DELHI has
completed his project report for the academic
year 2024-25 under the guidance and supervision
of Mrs.Divya Jyoti.
The practical work has been completed
independently, showing utmost sincerity in its
completion.

Teacher’s Signature
INDEX
S. NO. CONTENTS
1 Introduction
Creating/ Opening/
2
Closing binary files
3 Functions
Updating records in a
4
file
Codes/ Programmes/
5
Outputs
6 Bibliography
 INTRODUCTION
In a binary file, data is stored in a sequence of
bytes (8 bits), and these bytes can represent any
kind of data, such as images, audio, video,
executable programs, or even compressed data.
File Extension: .dat
Module: Pickle
Modes: rb, wb, ab

 Serialization (also called Pickling) is the process of converting


Python object hierarchy into a byte stream so that it can be
written into a file. Pickling converts an object in byte stream in
such a way that it can be reconstructed in original form when
unpickled or de-serialized.

 Unpickling is the inverse of Pickling where a byte stream is


converted into an object hierarchy. Unpickling produces the
exact replica of the original object.

# CREATING/ OPENING/ CLOSING BINARY FILES


With the file modes, ‘b’ is used to open a binary file. Python
provides the ‘pickle’ module to achieve this. It includes dump() and
load() methods to write and read from an open binary file,
respectively.

#Functions

Python provides two functions that help you manipulate


the position of the file-pointer and thus, we can read and
write from desired position in the file. These functions work
identically with the text and binary files.
I. Tell() : It returns the current position of the pointer in
the file. I t is used as per the following syntax: -
<file-object >. tell()
II. Seek() : It changes the position of the file-pointer by
placing the file-pointer at the specified position in the
open file.

# Updating records in a file


(i) Locate the record to be updated by searching for it.
(ii) Make changes in the loaded record in memory (the
read record).
(iii) Write back onto the file at the exact location of old
record.
 It is important to open the file in the read as well as
write mode.
 Write a program to a binary file called emp.dat
and write into it the employee details of some
employees, available in the form of dictionaries.

Import pickle

# Dictionary objects to be stored in the binary file

Emp1= {‘Empno’:1201, ‘Name’:’Anushree’,


‘Age’:25, ‘Salary’:47000}
Emp2= {‘Empno’:1202, ‘Name’:’Zoya’, ‘Age’:24,
‘Salary’:45000}
Emp3= {‘Empno’:1203, ‘Name’:’Anu’, ‘Age’:29,
‘Salary’:50000}

# Open file in write mode

empfile= open( ‘Emp.dat’,’wb’ )

# Write onto the file

pickle.dump( Emp1, empfile )


pickle.dump( Emp2, empfile )
pickle.dump( Emp3, empfile )
empfile.close() # Close file

 Write a program to open the file emp.dat and


read the objects written in it and display them.

import pickle
# Declare empty dictionary to hold the read
record.

Emp= {}

# Open binary file in read mode

Empfile= open(‘Emp.dat’, ‘rb’)


try: #Read repeatedly

while True:
Emp= pickle.load(Empfile)
print(Emp)
except:
Empfile.close() # Close file

# {‘Empno’:1201, ‘Name’:’Anushree’, ‘Age’:25,


‘Salary’:47000}
{‘Empno’:1202, ‘Name’:’Zoya’, ‘Age’:24,
‘Salary’:45000}
{‘Empno’:1203, ‘Name’:’Anu’, ‘Age’:2G,
‘Salary’:50000}

 Write a program to get student data (roll no.,


name and marks) from user and write onto a
binary file. The program should be able to get
data from the user.

import pickle
Stu= {} # declare empty dictionary

Stufile= open(‘Stu.dat’, ‘wb’) # open file

# get data to write onto the file

Rno= int(input(“Enter the roll number:”))


Name= input(“Enter name:”)
Marks= float(input(“Enter marks:”))

# add read data into dictionary


Stu[‘ Rollno ‘] = Rno
Stu[‘ Name‘] = Name
Stu[‘ Marks ‘] = Marks

# write into the file


pickle.dump( Stu, Stufile)
Stufile. close() # Close file

# Enter the roll number: 11


Enter name: Sia
Enter marks: 83.5

 Write the program to open file Stu.dat and


search for records with roll numbers 12 or 14. If
found, display the records.

import pickle
Stu= {}

# open binary file in read mode


Fin= open(‘Stu.dat’, ‘rb’)

# list contains key values to be searched for


Searchkeys = [12, 14]
try:
While True: #it will become False upon EOF

Stu = pickle.load(Fin)
if Stu[‘Rollno’] in Searchkeys:
print (Stu)
else:
#print the record
print (“Records not found”)
Fin.close() # Close file

#{ ‘Rollno’: 12, ‘Name’: ‘Guneet’, ‘Marks’: 80.5}


{ ‘Rollno’: 14, ‘Name’: ‘Gauri’, ‘Marks’: 84.5}

 Consider a binary file Stu.dat storing students’


record. Write a program to update the records of
the file so that those who have scored more than
81.0, get additional bonus marks of 2.

import pickle
Stu= {}
Fin= open(‘Stu.dat’, ‘rb+’)
#opening the file in the read as well as write mode
try:
While True:

#store file-pointer position before reading the


record.
Rpos = Fin.tell()
Stu = pickle.load(Fin)
If Stu [‘Marks’] > 81.0:
Stu [‘Marks’] += 2
# changes made in the record;
#place the pointer at the exact location of record;

Fin.seek(Rpos)

#now, write the updated record on the exact


location.
Pickle.dump(Stu, Fin)
except:
print(“EOF ERROR”)

Fin.close() # Close file

 Create a binary file namely Myfile.info and write


a string having two lines in it. Also, display the
string until letter ‘o’ is encountered.

import pickle
String= eval(input(‘Enter the string:”))
with open (“Myfile.info”, ‘wb’) as f:
pickle.dump(String, f)
with open (“Myfile.info”, ‘rb’) as f:
St= “”
St= pickle.load(f)
Ist = St.split(‘o’)
print( Ist[0])
#Enter the string: “This is my first line. This is
second line.”
This is my first line. This is sec

 Read the file Stu.dat and display records having


marks greater than 81.

Import pickle

# declare empty dictionary object to hold records;

Stu= {}
found= False

#open binary file in read mode and process with


the ‘with’ block;
with open(‘Stu.dat’, ‘rb’) as Fin:
Stu= pickle.load(Fin)

#read record in Stu dictionary from Fin file handle

if Stu[‘Marks’] >81 :
print(Stu)

#print the read record


If found== False:
Print(“ No records found”)
else:
Print(“Search successful”)

# {‘Rollno’: 11, ‘Name’: Sia, ‘Marks’: 83.50}


Search successful
BIBLIOGRAPHY

1. NCERT Textbook - Computer Science Class 12th.


Retrieved from
https://ncert.nic.in/textbook.php?lecs1=0-13.

2. Python Official Documentation - Python Software


Foundation. (2023). Python 3.10 Documentation.
Retrieved from https://docs.python.org/3.10/

3. W3Schools - Retrieved from


https://www.w3schools.com/sql

4. Computer Science with Python, Sumita Arora

You might also like