SlideShare a Scribd company logo
1 of 60
FILE HANDLING
 INTRODUCTION
 DATA FILES
 OPENINGANDCLOSING FILES
 READINGANDWRITING FILES
 STANDARD INPUT,OUTPUTAND ERRORSTREAMS
Introduction
 FILE HANDLING is a mechanism by which we
can read data of disk files in python program or
write back data from python program to disk
files.
 So far in our python program the standard input
in coming from keyboard an output is going to
monitor i.e. no where data is stored permanent
and entered data is present as long as program is
running BUT file handling allows us to store data
entered through python program permanently in
disk file and later on we can read back the data
DATA FILES
 It contains data pertaining to a specific
application, for later use. The data files can be
stored in two ways –
 Text File
 Binary File
Text File
 Text file stores information in ASCII OR
UNICODE character. In text file everything will
be stored as a character for example if data is
“computer” then it will take 8 bytes and if the
data is floating value like 11237.9876 it will take
10 bytes.
 In text file each line is terminated by special
character called EOL(End of line). In text file
some translation takes place when this EOL
character is read or written. In python EOL is ‘n’
or ‘r’ or combination of both
Binary files
 Binary files are stored in terms of bytes (0s
and 1s)
 It stores the information in the same format
as in the memory i.e. data is stored according
to its data type so no translation occurs.
 In binary file there is no delimiter for a new
line
 Binary files are faster and easier for a
program to read and write than text files.
 Data in binary files cannot be directly read, it
can be read only through python program for
the same.
File Pointer
 Every file maintains a file pointer which tells the
current position in the file where reading and
writing operation will take.
 When we perform any read/write operation two
things happens:
 The operation at the current position of file pointer
 File pointer advances by the specified number of
bytes.
Example
myfile = open(“ipl.txt”,”r”)
File pointer will be by default at first position i.e. first character
ch = myfile.read(1)
ch will store first character i.e. first character is consumed, and file pointer will
move to next character
Steps in Data File Handling
1. OPENING FILE
 We should first open the file for read or write by
specifying the name of file and mode.
2. PERFORMING READ/WRITE
 Once the file is opened now we can either read or
write for which file is opened using various functions
available
3. CLOSING FILE
 After performing operation we must close the file
and release the file for other application to use it,
Opening File
 File can be opened for either – read, write,
append.
SYNTAX:
file_object = open(filename)
Or
file_object = open(filename,mode)
** default mode is “read”
Opening File
myfile = open(“story.txt”)
here disk file “story.txt” is loaded in
memory and its reference is linked to “myfile”
object, now python program will access
“story.txt” through “myfile” object.
here “story.txt” is present in the same
folder where .py file is stored otherwise if disk
file to work is in another folder we have to give
full path.
Opening File
myfile = open(“article.txt”,”r”)
here “r” is for read (although it is by default, other
options are “w” for write, “a” for append)
myfile = open(“d:mydatapoem.txt”,”r”)
here we are accessing “poem.txt” file stored in
separate location i.e. d:mydata folder.
at the time of giving path of file we must use double
backslash() in place of single backslash because in python
single slash is used for escape character and it may cause
problem like if the folder name is “nitin” and we provide path
as d:nitinpoem.txt then in nitin “n” will become escape
character for new line, SO ALWAYS USE DOUBLE
BACKSLASH IN PATH
Opening a file using with clause
In Python, we can also open a file using with
clause. The syntax of with clause is:
with open (file_name, access_mode) as file_
object:
The advantage of using with clause is that any
file that is opened using this clause is closed
automatically,
Example:
with open(“myfile.txt”,”r+”) as myObject:
content = myObject.read()
File Access Mode
Text File
Mode
Binary File
Mode
Description Notes
‘r’ ‘rb’ Read only File must exists, otherwise Python raises
I/O errors
‘w’ ‘wb’ Write only If file not exists, file is created
If file exists, python will truncate
existing data and overwrite the file.
‘a’ ‘ab’ Append File is in write mode only, new data will
be added to the end of existing data i.e.
no overwriting. If file not exists it is
created
‘r+’ ‘r+b’ or ‘rb+’ Read and write File must exists otherwise error is raised
Both reading and writing can take place
w+ ‘w+b’ or ‘wb+’ Write and read File is created if not exists, if exists data
will be truncated, both read and write
allowed
‘a+’
VINOD K
SAC
‘a+b’ or ‘ab+’
UMARVERMA,PGT(CS),
HIN BHARDWAJ, PGT(CS)
Write and read
KV OEF KANPUR&
, KV NO.1TEZPUR
Same as above but previous content will
be retained and both read and write.
Closing file
 As reference of disk file is stored in file handle
so to close we must call the close() function
through the file handle and release the file.
myfile.close()
Note: open function is built-in function used
standalone while close() must be called through file
handle
Reading from File
 To read from file python provide many functions
like :
 Filehandle.read([n]) : reads and return n bytes,
if n is not specified it reads entire file.
 Filehandle.readline([n]) : reads a line of input.
If n is specified reads at most n bytes. Read bytes
in the form of string ending with line character or
blank string if no more bytes are left for reading.
 Filehandle.readlines(): reads all lines and
returns them in a list
Example-1: read()
SAMPLE FILE
Example-2: read()
SAMPLE FILE
Example-3: readline()
SAMPLE FILE
SAMPLE FILE
Example-4:
Example 5 To display data from a text
file
fin=open("practice.txt","r")
str = fin.readline()
while str:
print(str)
str=fin.readline() fin.close()
OUTPUT IS:
i am in mumbai
i like music
i love india
SAMPLE FILE
Example-6 & 7: counting size of file and count lines
Questions…
1) Write python code to display each word of a line separately as an
element of a list.
Answer:
myobject=open("myfile.txt",'r')
d=myobject.readlines()
for line in d:
words=line.split()
print(words)
Output is:
In the output, each string is
returned as elements of a list.
However, if splitlines() is used
instead of split(), then each line
is returned as element of a list,
as shown in the output below:
>>> for line in d:
words=line.splitlines()
print(words)
Output is:
[‘I am an indian’]
[‘My country is india’]
fobject=open("testfile.txt","w")
sentence=input("Enter the contents to
be written in the file: ")
fobject.write(sentence)
fobject.close()
print("Now reading the contents of
the file: ")
fobject=open("testfile.txt","r")
for str in fobject:
print(str)
fobject.close()
Writing and reading to a text file
Output is:
write() - for writing a single string
writeline() - for writing a sequence
of strings
The write() method
write() method takes a string as an
argument and writes it to the text file. It
returns the number of characters being
written on single execution of the write()
method.
writelines() method
This method is used to write multiple
strings to a file.
We need to pass an iterable object like lists,
tuple, etc. containing strings to the
writelines() method.
Unlike, write(), the writelines() method
does not return the
number of characters written in the file.
Setting Offsets in a File
The functions that we have learnt till now are used to
access the data sequentially from a file. But if we want
to access data in a random fashion, then Python gives us
seek() and tell() functions to do so.
The tell() method
This function returns an integer that specifies the
current position of the file object in the file. The position
so specified is the byte position from the beginning of
the file till the current position of the file object. The
syntax of using tell() is:
file_object.tell()
The seek() method
This method is used to position the file object at a particular
position in a file.
The syntax of seek() is:
file_object.seek(offset [, reference_point])
In the above syntax, offset is the number of bytes by which the
file object is to be moved.
Reference_point indicates the starting position of the file object.
That is, with reference to which position, the offset has to be
counted. It can have any of the following values:
0 - beginning of the file
1 - current position of the file
2 - end of file
By default, the value of reference_point is 0, i.e. the offset is
counted from the beginning of the file.
For example, the statement fileObject.seek(5,0) will position the
file object at 5th byte position from the beginning of the file.
Seek() function:
For example,
1. f.seek(–10,1) from current position,
move 10 bytes backward
2. f.seek(10,1) from current position,
move 10 bytes forward
3. f.seek(–20,1) from current position,
move 20 bytes backward
4. f.seek(10,0) from beginning of file,
move 10 bytes forward
Application of seek() and tell()
print("Learning to move the file object")
fileobject=open("testfile.txt","r+")
str=fileobject.read()
print(str)
print("Initially, the position of the file object is: ",fileobject.
tell())
fileobject.seek(0)
print("Now the file object is at the beginning of the
file:",fileobject.tell())
fileobject.seek(5)
print("The position of the file object is at", fileobject.tell())
str=fileobject.read()
print(str)
Output is:
Learning to move the file object
i am good
Initially, the position of the file
object is: 9
Now the file object is at the
beginning of the file: 0
The position of the file object is at
5
good
To create a text file and write
multiple lines in it
fileobject=open("practice.txt","w+")
while True:
data= input("Enter data to save in the text file: ")
fileobject.write(data)
ans=input("Do you wish to enter more data?(y/n): ")
if ans=='n': break
fileobject.close()
Output is:
Enter data to save in the text
file: I am interested to learn
about Computer Science
Do you wish to enter more
data?(y/n): y
Enter data to save in the text
file: Python is easy to learn
Do you wish to enter more
data?(y/n): n
To read and display data that is
stored in a text file :
fileobject=open("practice.txt","r")
str = fileobject.readline()
while str:
print(str)
str=fileobject.readline()
fileobject.close()
the readline() is used in the while
loop to read the data line by line
from the text file. The lines are
displayed using the print().
To perform reading and writing
operation in a text file using a+ mode
fileobject=open("report.txt", “a+")
print ("WRITING DATA IN THE FILE")
print() # to display a blank line
while True:
line= input("Enter a sentence ")
fileobject.write(line)
fileobject.write('n')
choice=input("Do you wish to enter more data? (y/n): ")
if choice in ('n','N'): break
print("The byte position of file object is
",fileobject.tell())
fileobject.seek(0) #places file object at beginning of file
print()
print("READING DATA FROM THE FILE")
str=fileobject.read()
print(str)
Output is:
WRITING DATA IN THE FILE
Enter a sentence=hello how are you?
Do you wish to enter more data?
(y/n): n
The byte position of file object is
34
READING DATA FROM THE FILE=
i love india
hello how are you?
Binary File Operation using
Pickle Module
To save any object structure along with
data, Python provides a module called
Pickle. The module Pickle is used for
serializing and de-serializing any
Python object structure.
Serialization is the process of
transforming data or an object in memory
(RAM) to a stream of bytes called byte
streams. These byte streams in a binary
file can then be stored in a disk or in
a database or sent through a network.
Serialization process is also called
pickling.
De-serialization or unpickling is the inverse of
pickling process where a byte stream is converted
back to Python object.
The pickle module deals with binary files. Here,
data are not written but dumped and similarly,
data are not read but loaded. The Pickle Module
must be imported to load and dump data. The
pickle module provides two methods - dump()
and load() to work with binary files for pickling
and unpickling, respectively.
Steps to perform binary file operations
 First we need to import the module called
pickle.
 This module provides 2 main functions:
 dump() : to write the object in file which is loaded
in binary mode
 Syntax : dump(object_to_write, filehandle)
 load() : dumped data can be read from file using
load() i.e. it is used to read object from pickle file.
 Syntax: object = load(filehandle)
dump () method
This method is used to convert (pickling) Python
objects for writing data in a binary file. The file in
which data are to be dumped, needs to be opened
in binary write mode (wb).
Syntax of dump() is as follows:
dump(data_object, file_object)
where data_object is the object that has to be
dumped to the file with the file handle named file
object.
Ex= write the record of a student
(roll_no, name, gender and marks)
in the binary file named
mybinary.dat using the dump(). We
need to close the file after
pickling
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()
Example: dump()
See the content is some kind
of encrypted format, and it is
not in complete readable
form
Program to insert/append a record
in the binary file "student.dat"
Program to read a record from the
binary file "student.dat"
Program to search a record from the
binary file "student.dat" on the
basis of roll number
Updating a record in the file requires roll
number to be fetched from the user whose name
& marks are to be updated.
import pickle
f=open('student.dat','rb+')
found=0
r=int(input('Enter roll no to search :'))
while True:
rec=pickle.load(f)
if rec['Rollno']==r:
print('Current name is:',rec['Name'])
print('Current Marks are:',rec['Marks'])
rec['Name']=input('Enter new name:')
rec['Marks']=int(input('Enter new marks:'))
found=1
break
if found==1:
f.seek(0)
pickle.dump(rec,f)
print('Record updated')
f.close()
The load() method
This method is used to load (unpickling) data from
a binary file. The file to be loaded is opened in
binary read(rb) mode.
Syntax of load() is as follows:
Store_object = load(file_object)
Here, the pickled Python object is loaded from the
file having a file handle named file_object and is
stored in a new file handle called store_object.
Example: load()
Example
Unpickling data in Python
import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
Output of Program 2-7:
>>>
RESTART: Path_to_fileProgram2-7.py
The data that were stored in file are:
[1, 'Geetika', 'F', 26]
Example-: Writing String as a record
to file
Example-: To copy the content of one
file to another file
Removing whitespaces after reading
from file
 read() and readline() reads data from file and
return it in the form of string and readlines()
returns data in the form of list.
 All these read function also read leading and
trailing whitespaces, new line characters. If you
want to remove these characters you can use
functions
 strip() : removes the given character from both ends.
 lstrip(): removes given character from left end
 rstrip(): removes given character from right end
Example: strip(),lstrip(),
rstrip()
File Handling:
CSV FILE (Comma separated value)
CSV (Comma Separated Values) is a file format for
data storage which looks like a text file. The
information is organized with one record on each
line and each field is separated by comma.
CSV File Characteristics:
• One line for each record
• Comma separated fields
• Space-characters adjacent to commas are
ignored
• Fields with in-built commas are separated by
double quote characters.
When to Use CSV?
• When data has a strict tabular
structure
• To transfer large database between
programs
• To import and export data to office
applications, Qedoc modules
• To store, manage and modify
shopping cart catalogue
CSV Advantages
• CSV is faster to handle
• CSV is smaller in size
• CSV is easy to generate
• CSV is human readable and easy to edit
manually
• CSV is simple to implement and parse
• CSV is processed by almost all existing
applications
CSV Disadvantages
• No standard way to represent binary
data
• There is no distinction between text and
numeric values
• Poor support of special characters and
control characters
• CSV allows to move most basic data only.
Complex configurations cannot be
imported and exported this way
• Problems with importing CSV into SQL
(no distinction between NULL and quotes)
Write / Read CSV FILE
Writing and reading operation from text file is
very easy.
First of all we have to import csv module for file
operation/method call.
For writing ,we open file in ‘w’ writing mode
using open() method which create newFile like
object.
Through csv.writer() method ,we create writer
object to call writerow() method to write objects.
Reading data from CSV file
Similarly for reading ,
we open the file in ‘r’ mode and create new
File like object, further we create new file
reader object using csv.reader()method to
read each row of the file.
Three file opening modes are there ‘w’,’r’,’a’.
‘a’ for append .
After file operation close opened file using
close() method.

More Related Content

Similar to FILE HANDLING.pptx

Similar to FILE HANDLING.pptx (20)

Python file handlings
Python file handlingsPython file handlings
Python file handlings
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Python-files
Python-filesPython-files
Python-files
 
Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
File Handling
File HandlingFile Handling
File Handling
 
working with files
working with filesworking with files
working with files
 
Python data file handling
Python data file handlingPython data file handling
Python data file handling
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

FILE HANDLING.pptx

  • 1. FILE HANDLING  INTRODUCTION  DATA FILES  OPENINGANDCLOSING FILES  READINGANDWRITING FILES  STANDARD INPUT,OUTPUTAND ERRORSTREAMS
  • 2. Introduction  FILE HANDLING is a mechanism by which we can read data of disk files in python program or write back data from python program to disk files.  So far in our python program the standard input in coming from keyboard an output is going to monitor i.e. no where data is stored permanent and entered data is present as long as program is running BUT file handling allows us to store data entered through python program permanently in disk file and later on we can read back the data
  • 3. DATA FILES  It contains data pertaining to a specific application, for later use. The data files can be stored in two ways –  Text File  Binary File
  • 4. Text File  Text file stores information in ASCII OR UNICODE character. In text file everything will be stored as a character for example if data is “computer” then it will take 8 bytes and if the data is floating value like 11237.9876 it will take 10 bytes.  In text file each line is terminated by special character called EOL(End of line). In text file some translation takes place when this EOL character is read or written. In python EOL is ‘n’ or ‘r’ or combination of both
  • 5. Binary files  Binary files are stored in terms of bytes (0s and 1s)  It stores the information in the same format as in the memory i.e. data is stored according to its data type so no translation occurs.  In binary file there is no delimiter for a new line  Binary files are faster and easier for a program to read and write than text files.  Data in binary files cannot be directly read, it can be read only through python program for the same.
  • 6. File Pointer  Every file maintains a file pointer which tells the current position in the file where reading and writing operation will take.  When we perform any read/write operation two things happens:  The operation at the current position of file pointer  File pointer advances by the specified number of bytes.
  • 7. Example myfile = open(“ipl.txt”,”r”) File pointer will be by default at first position i.e. first character ch = myfile.read(1) ch will store first character i.e. first character is consumed, and file pointer will move to next character
  • 8. Steps in Data File Handling 1. OPENING FILE  We should first open the file for read or write by specifying the name of file and mode. 2. PERFORMING READ/WRITE  Once the file is opened now we can either read or write for which file is opened using various functions available 3. CLOSING FILE  After performing operation we must close the file and release the file for other application to use it,
  • 9. Opening File  File can be opened for either – read, write, append. SYNTAX: file_object = open(filename) Or file_object = open(filename,mode) ** default mode is “read”
  • 10. Opening File myfile = open(“story.txt”) here disk file “story.txt” is loaded in memory and its reference is linked to “myfile” object, now python program will access “story.txt” through “myfile” object. here “story.txt” is present in the same folder where .py file is stored otherwise if disk file to work is in another folder we have to give full path.
  • 11. Opening File myfile = open(“article.txt”,”r”) here “r” is for read (although it is by default, other options are “w” for write, “a” for append) myfile = open(“d:mydatapoem.txt”,”r”) here we are accessing “poem.txt” file stored in separate location i.e. d:mydata folder. at the time of giving path of file we must use double backslash() in place of single backslash because in python single slash is used for escape character and it may cause problem like if the folder name is “nitin” and we provide path as d:nitinpoem.txt then in nitin “n” will become escape character for new line, SO ALWAYS USE DOUBLE BACKSLASH IN PATH
  • 12. Opening a file using with clause In Python, we can also open a file using with clause. The syntax of with clause is: with open (file_name, access_mode) as file_ object: The advantage of using with clause is that any file that is opened using this clause is closed automatically, Example: with open(“myfile.txt”,”r+”) as myObject: content = myObject.read()
  • 13. File Access Mode Text File Mode Binary File Mode Description Notes ‘r’ ‘rb’ Read only File must exists, otherwise Python raises I/O errors ‘w’ ‘wb’ Write only If file not exists, file is created If file exists, python will truncate existing data and overwrite the file. ‘a’ ‘ab’ Append File is in write mode only, new data will be added to the end of existing data i.e. no overwriting. If file not exists it is created ‘r+’ ‘r+b’ or ‘rb+’ Read and write File must exists otherwise error is raised Both reading and writing can take place w+ ‘w+b’ or ‘wb+’ Write and read File is created if not exists, if exists data will be truncated, both read and write allowed ‘a+’ VINOD K SAC ‘a+b’ or ‘ab+’ UMARVERMA,PGT(CS), HIN BHARDWAJ, PGT(CS) Write and read KV OEF KANPUR& , KV NO.1TEZPUR Same as above but previous content will be retained and both read and write.
  • 14. Closing file  As reference of disk file is stored in file handle so to close we must call the close() function through the file handle and release the file. myfile.close() Note: open function is built-in function used standalone while close() must be called through file handle
  • 15. Reading from File  To read from file python provide many functions like :  Filehandle.read([n]) : reads and return n bytes, if n is not specified it reads entire file.  Filehandle.readline([n]) : reads a line of input. If n is specified reads at most n bytes. Read bytes in the form of string ending with line character or blank string if no more bytes are left for reading.  Filehandle.readlines(): reads all lines and returns them in a list
  • 20. Example 5 To display data from a text file fin=open("practice.txt","r") str = fin.readline() while str: print(str) str=fin.readline() fin.close() OUTPUT IS: i am in mumbai i like music i love india
  • 21. SAMPLE FILE Example-6 & 7: counting size of file and count lines
  • 22. Questions… 1) Write python code to display each word of a line separately as an element of a list. Answer: myobject=open("myfile.txt",'r') d=myobject.readlines() for line in d: words=line.split() print(words) Output is:
  • 23. In the output, each string is returned as elements of a list. However, if splitlines() is used instead of split(), then each line is returned as element of a list, as shown in the output below: >>> for line in d: words=line.splitlines() print(words) Output is: [‘I am an indian’] [‘My country is india’]
  • 24. fobject=open("testfile.txt","w") sentence=input("Enter the contents to be written in the file: ") fobject.write(sentence) fobject.close() print("Now reading the contents of the file: ") fobject=open("testfile.txt","r") for str in fobject: print(str) fobject.close() Writing and reading to a text file
  • 26. write() - for writing a single string writeline() - for writing a sequence of strings The write() method write() method takes a string as an argument and writes it to the text file. It returns the number of characters being written on single execution of the write() method.
  • 27. writelines() method This method is used to write multiple strings to a file. We need to pass an iterable object like lists, tuple, etc. containing strings to the writelines() method. Unlike, write(), the writelines() method does not return the number of characters written in the file.
  • 28. Setting Offsets in a File The functions that we have learnt till now are used to access the data sequentially from a file. But if we want to access data in a random fashion, then Python gives us seek() and tell() functions to do so. The tell() method This function returns an integer that specifies the current position of the file object in the file. The position so specified is the byte position from the beginning of the file till the current position of the file object. The syntax of using tell() is: file_object.tell()
  • 29. The seek() method This method is used to position the file object at a particular position in a file. The syntax of seek() is: file_object.seek(offset [, reference_point]) In the above syntax, offset is the number of bytes by which the file object is to be moved. Reference_point indicates the starting position of the file object. That is, with reference to which position, the offset has to be counted. It can have any of the following values: 0 - beginning of the file 1 - current position of the file 2 - end of file By default, the value of reference_point is 0, i.e. the offset is counted from the beginning of the file. For example, the statement fileObject.seek(5,0) will position the file object at 5th byte position from the beginning of the file.
  • 30. Seek() function: For example, 1. f.seek(–10,1) from current position, move 10 bytes backward 2. f.seek(10,1) from current position, move 10 bytes forward 3. f.seek(–20,1) from current position, move 20 bytes backward 4. f.seek(10,0) from beginning of file, move 10 bytes forward
  • 31. Application of seek() and tell() print("Learning to move the file object") fileobject=open("testfile.txt","r+") str=fileobject.read() print(str) print("Initially, the position of the file object is: ",fileobject. tell()) fileobject.seek(0) print("Now the file object is at the beginning of the file:",fileobject.tell()) fileobject.seek(5) print("The position of the file object is at", fileobject.tell()) str=fileobject.read() print(str)
  • 32. Output is: Learning to move the file object i am good Initially, the position of the file object is: 9 Now the file object is at the beginning of the file: 0 The position of the file object is at 5 good
  • 33. To create a text file and write multiple lines in it fileobject=open("practice.txt","w+") while True: data= input("Enter data to save in the text file: ") fileobject.write(data) ans=input("Do you wish to enter more data?(y/n): ") if ans=='n': break fileobject.close()
  • 34. Output is: Enter data to save in the text file: I am interested to learn about Computer Science Do you wish to enter more data?(y/n): y Enter data to save in the text file: Python is easy to learn Do you wish to enter more data?(y/n): n
  • 35. To read and display data that is stored in a text file : fileobject=open("practice.txt","r") str = fileobject.readline() while str: print(str) str=fileobject.readline() fileobject.close() the readline() is used in the while loop to read the data line by line from the text file. The lines are displayed using the print().
  • 36. To perform reading and writing operation in a text file using a+ mode fileobject=open("report.txt", “a+") print ("WRITING DATA IN THE FILE") print() # to display a blank line while True: line= input("Enter a sentence ") fileobject.write(line) fileobject.write('n') choice=input("Do you wish to enter more data? (y/n): ") if choice in ('n','N'): break print("The byte position of file object is ",fileobject.tell()) fileobject.seek(0) #places file object at beginning of file print() print("READING DATA FROM THE FILE") str=fileobject.read() print(str)
  • 37. Output is: WRITING DATA IN THE FILE Enter a sentence=hello how are you? Do you wish to enter more data? (y/n): n The byte position of file object is 34 READING DATA FROM THE FILE= i love india hello how are you?
  • 38. Binary File Operation using Pickle Module To save any object structure along with data, Python provides a module called Pickle. The module Pickle is used for serializing and de-serializing any Python object structure. Serialization is the process of transforming data or an object in memory (RAM) to a stream of bytes called byte streams. These byte streams in a binary file can then be stored in a disk or in a database or sent through a network. Serialization process is also called pickling.
  • 39. De-serialization or unpickling is the inverse of pickling process where a byte stream is converted back to Python object. The pickle module deals with binary files. Here, data are not written but dumped and similarly, data are not read but loaded. The Pickle Module must be imported to load and dump data. The pickle module provides two methods - dump() and load() to work with binary files for pickling and unpickling, respectively.
  • 40. Steps to perform binary file operations  First we need to import the module called pickle.  This module provides 2 main functions:  dump() : to write the object in file which is loaded in binary mode  Syntax : dump(object_to_write, filehandle)  load() : dumped data can be read from file using load() i.e. it is used to read object from pickle file.  Syntax: object = load(filehandle)
  • 41. dump () method This method is used to convert (pickling) Python objects for writing data in a binary file. The file in which data are to be dumped, needs to be opened in binary write mode (wb). Syntax of dump() is as follows: dump(data_object, file_object) where data_object is the object that has to be dumped to the file with the file handle named file object.
  • 42. Ex= write the record of a student (roll_no, name, gender and marks) in the binary file named mybinary.dat using the dump(). We need to close the file after pickling import pickle listvalues=[1,"Geetika",'F', 26] fileobject=open("mybinary.dat", "wb") pickle.dump(listvalues,fileobject) fileobject.close()
  • 43. Example: dump() See the content is some kind of encrypted format, and it is not in complete readable form
  • 44. Program to insert/append a record in the binary file "student.dat"
  • 45. Program to read a record from the binary file "student.dat"
  • 46. Program to search a record from the binary file "student.dat" on the basis of roll number
  • 47. Updating a record in the file requires roll number to be fetched from the user whose name & marks are to be updated. import pickle f=open('student.dat','rb+') found=0 r=int(input('Enter roll no to search :')) while True: rec=pickle.load(f) if rec['Rollno']==r: print('Current name is:',rec['Name']) print('Current Marks are:',rec['Marks']) rec['Name']=input('Enter new name:') rec['Marks']=int(input('Enter new marks:')) found=1 break if found==1: f.seek(0) pickle.dump(rec,f) print('Record updated') f.close()
  • 48. The load() method This method is used to load (unpickling) data from a binary file. The file to be loaded is opened in binary read(rb) mode. Syntax of load() is as follows: Store_object = load(file_object) Here, the pickled Python object is loaded from the file having a file handle named file_object and is stored in a new file handle called store_object.
  • 50. Example Unpickling data in Python import pickle print("The data that were stored in file are: ") fileobject=open("mybinary.dat","rb") objectvar=pickle.load(fileobject) fileobject.close() print(objectvar) Output of Program 2-7: >>> RESTART: Path_to_fileProgram2-7.py The data that were stored in file are: [1, 'Geetika', 'F', 26]
  • 51. Example-: Writing String as a record to file
  • 52. Example-: To copy the content of one file to another file
  • 53. Removing whitespaces after reading from file  read() and readline() reads data from file and return it in the form of string and readlines() returns data in the form of list.  All these read function also read leading and trailing whitespaces, new line characters. If you want to remove these characters you can use functions  strip() : removes the given character from both ends.  lstrip(): removes given character from left end  rstrip(): removes given character from right end
  • 55. File Handling: CSV FILE (Comma separated value) CSV (Comma Separated Values) is a file format for data storage which looks like a text file. The information is organized with one record on each line and each field is separated by comma. CSV File Characteristics: • One line for each record • Comma separated fields • Space-characters adjacent to commas are ignored • Fields with in-built commas are separated by double quote characters.
  • 56. When to Use CSV? • When data has a strict tabular structure • To transfer large database between programs • To import and export data to office applications, Qedoc modules • To store, manage and modify shopping cart catalogue
  • 57. CSV Advantages • CSV is faster to handle • CSV is smaller in size • CSV is easy to generate • CSV is human readable and easy to edit manually • CSV is simple to implement and parse • CSV is processed by almost all existing applications
  • 58. CSV Disadvantages • No standard way to represent binary data • There is no distinction between text and numeric values • Poor support of special characters and control characters • CSV allows to move most basic data only. Complex configurations cannot be imported and exported this way • Problems with importing CSV into SQL (no distinction between NULL and quotes)
  • 59. Write / Read CSV FILE Writing and reading operation from text file is very easy. First of all we have to import csv module for file operation/method call. For writing ,we open file in ‘w’ writing mode using open() method which create newFile like object. Through csv.writer() method ,we create writer object to call writerow() method to write objects.
  • 60. Reading data from CSV file Similarly for reading , we open the file in ‘r’ mode and create new File like object, further we create new file reader object using csv.reader()method to read each row of the file. Three file opening modes are there ‘w’,’r’,’a’. ‘a’ for append . After file operation close opened file using close() method.