The document discusses file handling in Python, specifically focusing on the use of the seek() function to change the current file position. It includes example code for writing to and reading from files, as well as searching for records in a binary file using the pickle module. The document also mentions methods for appending data and handling employee records.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views5 pages
Binary File
The document discusses file handling in Python, specifically focusing on the use of the seek() function to change the current file position. It includes example code for writing to and reading from files, as well as searching for records in a binary file using the pickle module. The document also mentions methods for appending data and handling employee records.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
PLE E MOE}
frremove le [Link] -
Sw rename backupsxttosample-st
er -anslation eur.
testores information inthe same formatasinthe memory tusnotranslat
= 1d write than text files.
2 Thereis no delimiter fora
are faster andeaserforaprogramtoread an
ge cue compres versions fer
iQ method of python tells us the current position of the cursor within the fil,
The seek) Function
The seek(offset, om) method changes the current file postion.
ly hrough python program forthe am
‘The offset argument indicates the number of bytes to be moved,
Example Code:
= opent"adat’, wb)
ine ='G20 Presidency\nOne Earth, One Family, One Future’
write(line)
close()
en(“atxt’ ‘be print(étellQ)
print(tread(7)) # read seven characters print(Etell())
print(EreadQ))
print(EtellQ)
fseek{9,0) # moves to 9 position from beginning
print(Eread(s))
fseek(4, 1) # moves to 4 position from current location
print (Eread(5))
{seek(-5, 2) # Goto the Sth byte before the e
rint(fread(5)) 7
YT REIoCit) Tien 24/06/2025 08:37n Me ace:
se et hat
fC an
ease Oa) pen ee wrtog i oar oe
iopenCemployesat 2 in binary
eraprond date to add more records, Instead of w” write’
ictneey oop win tlle ofan
osin(put er empaye umber)
‘Sects caploee sane)
‘Scnput Ene slay)
txparpend{eanamesl) Hemp i etd ssf sts by appending ito
ncn Want to enter mere records press 9)
pidiedumplemp.). wrtelisemp to ug le object
Fao
>>> stil be rested and tore inf inthe lowing form
>>I on 100760 [5 hn 9067S ua Samra
4 Program 2: Reading data from file
import pickle
emp=[]
fropent“employeedat’,"r5")
‘returns data as list of lists each lst represent one record “%
emp=plekledoad()
print(emp)[Link]()
# Program 3: searching a record from file
import pickle
emp=[]
fopen(“[Link]”,
e=int(input(“enter employee no you want to sear
emp=pickleload(f)
foun
ford in emp:
ifd[O}==e:
rint(d)
found=1
PLUTO TD
break
i) ien :(i>) te 24/06/2025 08:37jaeIeYD-a0edg
SPI®y paesedos eunu0)
P0204 y>e9 10) uy aug
AEST,
‘sonsumperey apy
-aseqeyep e
TWSAW "9}2810 ~2xg) saseqeiep pue (aqy
mate ANY, ep Jo Siunowe any a;puey
“Suoneotidde wasayp waamiaq
in
2Boyspeaids & ony yep rioduy oy pasn aq ues
[P9x9 ~3x9) syaayspeauds wou e7ep uuodx on
ep suresBoud fq poweo1a fyensn aie say. nsy
"itp BurBueyaxs 10j pasn uayo ase soqy asoy,
“euep parezedas-euruto> aya suyequo> sey ay 2x0 ed ws)
“euuiuoa fq payesedas
hay YDeD pue ouy Yove UI p1oda1 auo yarm paziuedLo s1 uonewoju ayy “a1y yor
Ya aBer0ys wep 10) yeULIOS aIY v SI
asada 04, seunuoD £q paresedas 5}
Bp 91015 01 sem uouruzo> pue ajdusis
Inges ut
ut at J0 auo s] zeMLOy (SonyeA parexEdas euOD) As
TAS
2e-9EUOWAG\ \uoUrKs\\suresfo14\ \e>07\\eweaddy\ \aNINA\\SH2SN\9,
Axoro244p Bupyio% qua11N9 249 Jo yped ayp SuUyEWWo> Bus e
Ci) Eis