Binary Files
Binary Files
INTRO:
Extension= .dat
Most of the computer files are of this format. Eg:.docx , .png , .mp3
Encoded in binary format which is only understood by a computer or a
machine.
Needn’t be translated as the computer can understand it as such.
No delimiter to end a line
PICKLE:
It is a module imported to read and write into a binary file.
Used for:
o Pickling(serializing) = Process of conversion of data structures in
RAM into byte streams stored in databases.
o Unpickling(deserialIzing) =Processs of conversion of byte stream into
original data structures
Pickling is required at the time of reading and writing in a binary file.
Syntax:
import pickle
Contains 2 functions:
Load(): Used for reading contents from the files.
Reads one value at a time,hence need to be called each
time dump is used
Need to use loop.
Reading mode =’rb’
Syntax:
object = load(fileobject)
variable= pickle.load(fileobject)
dump(): Used for writing contents into the file.
Dumps altogether at a time.
Writing mode=’ wb’
Syntax:
Pickle.dump(object/sequence,fileobject)
INSERTION:
Use dump().
SEARCH:
o Use load().
UPDATION:
o Use load().
DELETION:
SEEK():
o It is used to place the filepointer or cursor at a specified
position.
o Data will be read from this position onwards.
o Used in 2 ways:
Absolute positioning: syntax is-
f.seek(position).The cursor will move that
many bytes from the beginning irrespective of
the current position of the cursor.
Relative positioning:syntax is-
f.seek(offset,from_what).This will move the
cursor from the specified position according to
the offset/bytes specified[(+)-moves fwd,(-) -
moves bkd]
o from_what can take 3 values:
0= beginning of the file
1=current cursor position
2=end of the file
No arg=beginning
TELL():
o Gives the current position of the cursor.
o Syntax is
f.tell()
o If the file is opened in:
Reading/writing mode = cursor at the beginning
Appending mode= cursor at the end of the file