0% found this document useful (0 votes)
111 views44 pages

Presentation 1

File handling in Python allows us to store and retrieve data from files. There are two main types of files - text files which store data in human-readable format using characters and have the .txt extension, and binary files which store data in its actual memory format and are used for images, audio/video etc. The CSV file format is commonly used to store tabular data in a spreadsheet-like format, with values separated by commas by default. Python's CSV module provides objects to easily read from and write to CSV files to import/export data.

Uploaded by

PawanKumar
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)
111 views44 pages

Presentation 1

File handling in Python allows us to store and retrieve data from files. There are two main types of files - text files which store data in human-readable format using characters and have the .txt extension, and binary files which store data in its actual memory format and are used for images, audio/video etc. The CSV file format is commonly used to store tabular data in a spreadsheet-like format, with values separated by commas by default. Python's CSV module provides objects to easily read from and write to CSV files to import/export data.

Uploaded by

PawanKumar
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/ 44

FILE

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>)

File Handle or Path to Mode of a


File Object file file
Example
F=open(“myfile.txt”, “r”)

F=open(“C:\\Users\\Pawan Kumar\\Desktop\myfile.txt” , “r”)

F=open(r“C:\Users\Pawan Kumar\Desktop\myfile.txt” , “r”)


File Access Mode
Reading data from file
• read()
• readline()
• readlines()
read()
• Reads at most n bytes; if no n is
specified, reads the entire file.
• Returns the read bytes in the form of
a string.
• Syntax:
filehandle.read([n])
readline()
• Reads a line of input; if n is specified reads at
most n bytes.
• Return the read bytes in the form of string
ending with ‘\n’ character.
• Return a blank string if no more bytes are left
for reading in the file.
• Syntax:
filehandle.readline()
readlines()
• Read all lines of a text file.
• Returns in the form of list.
• Syntax:
filehandle.readlines()
How to write data in a file?
• write()
• writelines()
• Note- while writing data in a file
– if file doesn’t exist, it will create the new file.
– if file exist, it will write the data in a file , and
previous contents will be erased.
Write(string)
• Write() method takes a string and writes
it in a file
• For storing data, with EOL character, we
have to add ‘\n’ character to the end of
the string.
• For numeric value, we have to either
convert it into string using str() or write
in quotes.
writelines(seq)
• writelines() method is used to
write sequence data types in a
file (string, list, tuples)
Appending data to a file
• append means to add the data at the
end of file using ‘a’ mode.
• if file doesn’t exist, it will create the
new file.
• if file exist, it will append the append
the data at the end of a file.
Closing Files
• A close() function breaks the link
between file object and file on disk.
• After close(), no tasks can be
performed on that file through file
object.
• Syntax:
– filehandle.close()
• Write a method in Python to read
the content from text file “Poem.txt”
line by line and display the same on
the screen
• Write a method in Python to read
lines from a text file “poem.txt” and
display those lines which are starting
with an alphabet “O”
• Write a method in Python to count
the number of lines from text file
“Poem.txt” which are starting with
an alphabet “O”
• Write a method in Python BIGLINE()
to read lines from a text file
“Poem.txt” and display those lines,
which are bigger than 50 characters
• Write a method in python to
count the total number of words
in a file “India.txt”.
• Write a method in Python to read
lines from a text file “India.txt” to
find and display the occurrence of
the word ‘India’
L E S
F I
R Y
I N A
B
Binary Files
• Most of the files that we see in our computer
system are called binary files.
• Example:
• Image files: .png, .jpg, .gif, .bmp, etc.
• Video files: .mp4, .3gp, .mkv, .avi, etc.
• Audio files: .mp3, .wav, .mka, .aac, etc.
• Archive files: .zip, .rzr, .iso, etc.
• Executable files: .exe, .dll, .class, etc.
Binary Files (Cont….)
• We can open some binary files in the normal text
editor but we can’t read the content present inside the
file.
• That’s because all the binary files will be encoded in
the binary format, which can be understood only by a
computer or a machine.
• In binary files, there is no delimiter to the end a line
• Since they are directly in the form of binary, hence
there is no need to translate them.
• That’s why these files are easy and fast in working
Pickling
• Pickling refers to the process
of converting the structure
(such as list or dictionary) to a
byte stream before writing to
the file.
Pickling

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

You might also like