Presentation 1
Presentation 1
HANDLING
Why we need of File Handling?
• A file in itself is a bunch of bytes
stored on some storage devices.
• File helps us to store the data
permanently, which can be retrieved
for future use.
Types of Files
Binary
Text Files
Files
CSV Files
Text Files
• Stores information in ASCII or Unicode
characters.
• Each line of text is terminated with a
special character known as EOL (End of
Line)
• Extension for text file is .txt
• Default mode of file.
Binary Files
• Contains information in the same format
in which the information is held in
memory.
• There is no delimiter for a line.
• No translation are required
• More secure
Difference between Text Files and Binary Files
Text Files Binary Files
• Text file stores information in • Binary files are used to store
ASCII characters. binary data such as images, audio,
• Each line of text is terminated video, etc.
with a special character known as • There is no delimiter in Binary files
EOL (End of Line) • Binary files are difficult to
• Text files are easy to understand understand.
because these files are in human • Binary files are faster and easier
readable form. for a program to read and write
• Text files are slower than binary than the text files
files. • Binary files are having extension
• Text files having extension .txt .dat
How to open a text file?
FileObject=open(filename)
OR
FileObject=open(filename,<mode>)
Structur
e (List or Pickli Byte
Dictiona ng Stream
ry)
Unpickling
• Unpickling refers to the
process of converting the
byte stream back to the
original structure.
Unpickling
Byte Structur
Unpickl e (List or
Strea ing Dictiona
m ry)
Piclkle.dump()
• Used to write the object in a file.
• Syntax:
Pickle.dump(<structure>,file object)
Where,
Structure can be any sequence of python. It
can be either list or dictionary.
File object is the file handler of the file, in
which we want to write.
Pickle.load()
• Used to read the data from a
file.
• Syntax:
Structure=pickle.load(fileobject)
Write in a Binary File
Read from a Binary File
Append in a Binary File
Search in a Binary File
Update record in a Binary File
Delete Record from Binary File
E S
I L
F
S V
C
• CSV stands for Comma Separated Values.
• CSV is just like a text file, in a human
readable form which is extensively used to
store tabular data in a spreadsheet or
databases.
• The separator character of CSV files is
called a delimiter. Default delimiter is
comma(,). Other delimiters are tab(\t),
colon(:), pipe(|) and semicolon(;)
Separated
Separated
It is used f
Values
It tabular
is used d
f
Values
tabular
spreadsd
Comma
spreads
datab
Comma
datab
CSV Files
is called
Commas(
record.
is called
Commas(
record.
of fields se
of fields
Each se
reco
Each reco
aa
Advantages of CSV Files
Capable of
Storing large
amount of
Preferred import data
and export format
for databases and
spreadsheets.
Easier
To
Create
Python CSV Module
• CSV Module provide two types of
objects:
Reader- to read from the csv files
Writer- to write in to the csv files.
• To import CSV Module in our program,
write the following statement:
Import csv