0% found this document useful (0 votes)
8 views30 pages

Binary Files

The document explains the concepts of relative and absolute paths in file systems, detailing how to navigate directories to access files. It also contrasts binary files with text files, highlighting their differences in readability, storage format, and the use of the Pickle module for serializing Python objects. Additionally, it provides code examples for writing, reading, searching, and updating records in binary and CSV files.

Uploaded by

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

Binary Files

The document explains the concepts of relative and absolute paths in file systems, detailing how to navigate directories to access files. It also contrasts binary files with text files, highlighting their differences in readability, storage format, and the use of the Pickle module for serializing Python objects. Additionally, it provides code examples for writing, reading, searching, and updating records in binary and CSV files.

Uploaded by

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

Relative & absolute Paths : path is a sequence of directory names separated by

backslash from top most directory to all nested sub-directories followed by file name or
directory name.

C:\Users\hp\Desktop\photo.jpeg <------ path of photo.jpeg

Drive letter Root directory file

To reach the photo.jpeg file the sequence we should follows as :


under C drive there is Users(root folder) inside this there is hp sub-directory inside this
there is Desktop sub-directory and inside this we get the photo.jpeg file.

Absolute path is a path-name which start from top most level of directory structure
while relative path is related to current working directory.
So full path name is the absolute path which is denoted as mentioned .
Relative pathname shows the two dots for current working directory followed by back slash
and file-name.

In os.path module two functions abspath() and relpath() function is used to show the
absolute and relative path of any file.
Here is example to show this.

>>> import os

>>> os.path.abspath('example1.txt') 'C:\\Users\\Dell\\AppData\\Local\\Programs\\


Python\\Python37-32\\example1.txt’

>>>os.path.relpath('C:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\P ython37-32\\
Programs', 'example1.txt') '..\\Programs'
BINARY FILES
Comparison in between Binary Files and Text Files
Text files: In this type of file, Each line of text is terminated with Binary files: In this type of file, there is no terminator for a
a special character called EOL (End of Line), which is the new line and the data is stored after converting it into machine
line character (‘\n’) in python by default. understandable binary language.
Text files Binary files
Human Readable Format Machine Readable Format
Data stored in text format Data stored in machine language
Each line of text is terminated with a special
character called EOL (End of Line), which is the There is no terminator for a line
new line character (‘\n’) in python by default.
Slow files due to the Internal Translation from HLL Fast because data is stored in Machine Language
to LLL and no translation
Used for storing text only Used for storing Python objects
No specific Module required “Pickle” module is required
Binary files: In this type of file, there is no
terminator for a line and the data is stored after
converting it into machine understandable binary
language.
• Binary files used to read and write text, images or
audio and video file formats.
• Binary files read and write the data in the form of
bytes.
• Binary files are not in human readable format as
information in binary code.
• Many operation like search, update, insertion and
deletion of record can be done in binary file
• Used for writing and reading python objects like
list, tuples, dictionaries, class etc.
Binary files can be open using
1. open() method
2.‘with’ statement.

Various modes of binary files are ‘rb’, ’wb’, ’ab’, ’rb+’, ’wb+’ and ‘ab+’
PICKLE MODULE
Pickling -Also called serializing process of convert Python objects like class, list, tuple, dictionary into binary data in the
forms of bytes
To write in a file we use dump()
So data associated to these objects write into binary file using the pickling. Pickling is done using dump()
method of pickle module
pickle.dump(object, file-object)

Unpickling- It is the reverse process of pickling or conversion of byte stream into a Python objects
To read in a file we use load()
when the objects are stored into binary file, we can read it by converting byte stream back into python objects this
process is known as unpickle. Unpickling is done by using the load() functions of pickle module.
object=pickle.load(file-object)
The load() function reads byte streams from binary file convert and return the object.

Example to open file: fbw = open(“student.dat”,”wb”)

wb  write in binary file


rb  read in binary file
wb+  write & append in binary file
ab+  append & create in binary file
WRITING IN A BINARY FILE

import pickle
dct={}
fbw=open("E:\\python files\\fd.dat","wb")

ans='y'

while ans=='y':
roll = int(input("enter roll number"))
name = input("enter name")
mks = float(input("enter marks"))

dct['roll']=roll
dct['name']=name
dct['mks']=mks
pickle.dump(dct,fbw)
ans=input("enter more records")

fbw.close()
Reading from A BINARY FILE

import pickle
dct={}
fbr=open("E:\\python files\\fd.dat","rb")

try:
while True:
dct=pickle.load(fbr)
print(dct)

except EOFError:
fbr.close()
WRITING, READING, SEARCH
&
UPDATION
IN
BINARY DATA FILES
#program to write data in CSV files

import csv

fh=open("E:\\python files\\student.csv","w")

studwriter=csv.writer(fh)

studwriter.writerow(['Roll NO','NAME','MARKS’])

for i in range(5):
print("Students record")
rno=int(input("Enter roll no."))
name=input("enter name")
mk=float(input("Enter marks"))
sturec=[rno,name,mk]
studwriter.writerow(sturec)

fh.close()
DAYTA WRITING IN FILE

MEMORY (RAM)
List:-record
List variable data 0 1 2
roll name marks BINARY FILE
STUD.DAT

STORED AT HDD
#Data reading from file

import pickle
frb=open("E:\python files\stud.dat","rb")
record=pickle.load(frb)
print("contents of records are")
try:
for i in record:
roll_no=i[0]
name=i[1]
marks=i[2]
print(roll_no, name,marks)
except EOFError:
frb.close()
DATA READING FROM FILE

MEMORY (RAM)

List:-record
0 1 2
BINARY FILE
roll name marks 10 ROHAN 585 STUD.DAT

11 AMIT 579 STORED AT HDD

12 ARHAM 582
#Search operation in File

import pickle
frb=open("E:\python files\stud.dat","rb")
studrec=pickle.load(frb)
found=0
rno=int(input("enter the roll no. to be search:"))
for i in studrec:
if i[0]==rno:
print("Record found in database")
found=1
break
if found==0:
print("record not found")
frb.close()
SEARCH OPERATION IN DATA FILE

MEMORY (RAM)

List:-record
0 1 2 BINARY FILE
STUD.DAT
10 ROHAN 585
Variableroll STORED AT HDD
11 AMIT 579

12 ARHAM 582
#Updation of records in file
import pickle
frb=open("E:\python files\stud.dat","rb+")
studrec=pickle.load(frb)
found=0
rno=int(input("enter the roll no. to be search:"))
for i in studrec:
if i[0]==rno:
print("Record found in database")
print("current name is :",i[1])
i[1]=input("new name:")
found=1
break
if found==1:
frb.seek(0)
pickle.dump(studrec,frb)
print("Name updated!!!")

frb.close()
UPDATION IN DATA FILE

MEMORY (RAM)

List:-record
BINARY FILE
0 1 2
i [0] i [1] i [2] STUD.DAT
10 ROHAN 585
Variableroll STORED AT HDD
11 amrita 579

12 ARHAM 582
Programs for practice

1. WAP to Insert a new record in file by using lists


2. WAP to create a menu driven program which perform Insert, search, show and update operations
by using lists.

3. WAP to delete a record from file by using lists.

4. WAP to Insert a new record in file by using dictionary

5. WAP to create a menu driven program which perform Insert, search, show and update operations

by using dictionary.

6. WAP to delete a record from file by using dictionary.

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