File Handling Btech computer science and engineering ppt

pinuadarsh04 17 views 66 slides May 26, 2024
Slide 1
Slide 1 of 66
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66

About This Presentation

Data is very important. Every organization depends on its data for continuing its business operations. If the data is lost, the organization has to be closed. To store data in a computer, we need files. For example, we can store employee data like employee number, name and salary in a file in the...


Slide Content

Course Id :INT 213

FILE HANDLING

I INTRODUCTION

FILES
•Dataisveryimportant.Everyorganizationdependsonitsdatafor
continuingitsbusinessoperations.Ifthedataislost,theorganization
hastobeclosed.
•Thisisthereasoncomputersareprimarilycreatedforhandlingdata,
especiallyforstoringandretrievingdata.Inlaterdays,programsare
developedtoprocessthedatathatisstoredinthecomputer.

FILES
•Tostoredatainacomputer,weneedfiles.Forexample,wecan
storeemployeedatalikeemployeenumber,nameandsalaryinafile
inthecomputerandlateruseitwheneverwewant.
•Similarly,wecanstorestudentdatalikestudentrollnumber,name
andmarksinthecomputer.Incomputers’view,afileisnothingbut
collectionofdatathatisavailabletoaprogram.Oncewestoredata
inacomputerfile,wecanretrieveitanduseitdependingonour
requirements.

ADVANTAGES OF STORING A DATA IN A FILE
•Whenthedataisstoredinafile,itisstoredpermanently.Thismeans
thateventhoughthecomputerisswitchedoff,thedataisnot
removedfromthememorysincethefileisstoredonharddiskorCD.
Thisfiledatacanbeutilizedlater,wheneverrequired.
•Itispossibletoupdatethefiledata.Forexample,wecanaddnew
datatotheexistingfile,deleteunnecessarydatafromthefileand
modifytheavailabledataofthefile.Thismakesthefilemoreuseful.

ADVANTAGES OF STORING A DATA IN A FILE
•Oncethedataisstoredinafile,thesamedatacanbesharedby
variousprograms.Forexample,onceemployeedataisstoredina
file,itcanbeusedinaprogramtocalculateemployees’netsalaries
orinanotherprogramtocalculateincometaxpayablebythe
employees.
•Filesarehighlyusefultostorehugeamountofdata.Forexample,
voters’listorcensusdata.

II TYPES OF FILES

TYPES OF FILES
•InPython,therearetwotypesoffiles.
•Theyare:
Textfiles
Binaryfiles
•Textfilesstorethedataintheformofcharacters.Forexample,ifwe
storeemployeename“Ganesh”,itwillbestoredas6charactersandthe
employeesalary8900.75isstoredas7characters.
•Textfilesareusedtostorecharactersorstrings.

TYPES OF FILES
•Binaryfilesstoreentiredataintheformofbytes,i.e.agroupof8bits
each.Forexample,acharacterisstoredasabyteandanintegeris
storedintheformof8bytes(ona64bitmachine).Whenthedatais
retrievedfromthebinaryfile,theprogrammercanretrievethedata
asbytes.
•Binaryfilescanbeusedtostoretext,images,audioandvideo.
Imagefilesaregenerallyavailablein.jpg,.gifor.pngformats.
•Wecannotusetextfilestostoreimagesastheimagesdonot
containcharacters.

TYPES OF FILES
•Ontheotherhand,imagescontainpixelswhichareminutedotswith
whichthepictureiscomposedof.
•Eachpixelcanberepresentedbyabit,i.e.either1or0.Sincethese
bitscanbehandledbybinaryfiles,wecansaythattheyarehighly
suitabletostoreimages.Itisveryimportanttoknowhowtocreate
files,storedatainthefilesandretrievethedatafromthefilesin
Python

III READ A FILE

OPENING A FILE
•file = open("file1.txt","rb")
print(file)

MODES OF FILES

IV WRITING A DATA INTO A FILE

(a) write() method
•The write method is used to write
a string to an already opened file.
•String may include members,
special characters, other symbols.
f = open('file1.txt', 'w')
#enter characters from keyboard
str = input('Enter text:')
#write the string into file
f.write(str)
#closing the file
f.close()

writelines() method
•writelines() method is used
to write a list of strings.
•f = open('file1.txt', 'w’)
lines=["hello world,","welcometo the world
of python"]
f.writelines(lines)
#closing the file
f.close()
print("data written to file")

V APPEND DATA TO FILE

append() method
•To append a file, you must open it
using ‘a’ or ‘ab’ mode depending
on whether it is a text or a binary
file.
•f = open('file1.txt', 'a’)
f.write("\n my name is dev")
#closing the file
f.close()
print("data written to file")

V READ , READLINE, READLINES METHODS

(a) read() method
•Thismethodisusedtoreada
stringfromanalreadyopened
file.
•f = open('file1.txt', 'r’)
print(f.read(10))
f.close()

(b) readline() method
•This method is used to read a
single line from the file.
•f = open('file1.txt', 'r’)
print(f.readline())
print(f.readline())
print(f.readline())
f.close()

(c) readlines() method
•This method is used to read all
the lines in a file.
•f = open('file1.txt', 'r’)
print(f.readlines())
f.close()

VI DISPLAY THE CONTENTS OF A FILE USING LOOP

Display the contents of a file using FOR loop
•f = open('file1.txt', 'r’)
for line in f:
print(line)
f.close(

VII OPENING A FILE USING with KEYWORD

With Keyword
•with open ("file1.txt","rb") as file:
for line in file:
print(line)
file.close()
•with open ("file1.txt","r") as file:
for line in file:
print(line)
file.close()

VIII SPLITTING WORDS

split() function
•This function is used to split the
strings into words.
•with open ("file1.txt","b") as file:
line= file.readline()
words= line.split()
print(words)

IX EXCERCISE

1. What is the output of the Code?

Program 1
•Writeaprogramthatacceptsfilenameasaninputfromtheuser.
Openthefileandcountthenumberoftimesacharacterappearsin
thefile

Program 2
•Write a program that reads data from a file and calculates the
percentage of vowels and consonants

Program 3
•Write a program to count number of lines, words, characters in a
text file

X RENAMING AND DELETING

rename() method
•Rename () method takes two
arguments, the current filename
and the new filename.
•Import os
os.rename(“fileo.txt”,”filen.txt”)
print(“file renamed”)

remove() method
•Remove() method is used to
delete file.
•Import os
os.remove(“file1.txt”)
print(“file deleted”)

XI PICKLE

PICKLE
•Textfilesareusefulwhenwedonotwanttoperformany
calculationsonthedata.Whathappensifwewanttostoresome
structureddatainthefiles?Forexample,wewanttostoresome
employeedetailslikeemployeeidentificationnumber(inttype),
name(stringtype)andsalary(floattype)inafile.Thisdataiswell
structuredandgotdifferenttypes.Tostoresuchdata,weneedto
createaclassEmployeewiththeinstancevariablesid,nameandsal
asshowninnextslide.

PICKLE

Pickle
•Inthepreviousprogram,wecreateanobjecttoclassandstore
actualdataintothatobject.Later,thisobjectshouldbestoredintoa
binaryfileintheformofbytes.Thisiscalledpickleorserialization.
•So,let’sunderstandthatpickleisaprocessofconvertingaclass
objectintoabytestreamsothatitcanbestoredintoafile.Thisis
alsocalledobjectserialization.

Pickle
•Picklingisdoneusingthedump()methodof‘pickle’moduleas:
pickle.dump(object,file)
•Theprecedingstatementstoresthe‘object’intothebinary‘file’.
Oncetheobjectsarestoredintoafile,wecanreadthemfromthe
fileatanytime.

Pickle Implementation

Unpickle
•Unpickleisaprocesswherebyabytestreamisconvertedbackintoa
classobject.Itmeans,unpicklingrepresentsreadingtheclassobjects
fromthefile.
•Unpicklingisalsocalleddesearialization.
•Unpicklingisdoneusingtheload()methodof‘pickle’moduleas:
object=pickle.load(file)
•Here,theload()methodreadsanobjectfromabinary‘file’andreturns
itinto‘object’.Let’srememberthatpicklingandunpicklingshouldbe
doneusingbinaryfilessincetheysupportbytestreams.Theword
streamrepresentsdataflow.So,bytestreamrepresentsflowofbytes.

Unpickle Implementation

Picking and Unpickling a class object

XII seek() AND tell() method

tell() method
•Weknowthatdatainthebinaryfilesisstoredintheformofbytes.
Whenweconductreadingorwritingoperationsonabinaryfile,afile
pointermovesinsidethefiledependingonhowmanybytesare
writtenorreadfromthefile.
•Forexample,ifweread10bytesofdatafromafile,thefilepointer
willbepositionedatthe10thbytesothatitispossibletocontinue
readingfromthe11thbyteonwards.
•Toknowthepositionofthefilepointer,wecanusethetell()
method.

tell() method
•Itreturnsthecurrentpositionofthefilepointerfromthebeginning
ofthefile.Itisusedintheform:n=f.tell()
•Here,‘f’representsfilehandlerorfileobject.‘n’isanintegerthat
representsthebytepositionwherethefilepointerispositioned.In
case,wewanttomovethefilepointertoanotherposition,wecan
usetheseek()method.

seek () method
•Thismethodtakestwoarguments:f.seek(offset,fromwhere)
•Here,‘offset’representshowmanybytestomove.‘fromwhere’
representsfromwhichpositiontomove.
•Forexample,‘fromwhere’canbe0,1or2.Here,0representsfrom
thebeginningofthefile,1representsfromthecurrentpositionand
2representsfromtheendingofthefile.Thedefaultvalueof
‘fromwhere’is0,i.e.beginningofthefile.

seek() method
•f.seek(10) #same as f.seek(10, 0)
•This will move the file pointer to the 11th byte (i.e. 10+1) from the
beginning of the file (0 represents beginning of the file). So, any
reading operation will read data from 11th byte onwards.
•f.seek(-10, 2)
•This will move the file pointer to the 9th byte (-10+1) from the ending
of the file (2 represents ending of the file). The negative sign before
10 represents moving back in the file.

XIII RANDOM ACCESSING OF BINARY FILES USING
mmap

mmap
•mmap–‘memorymappedfile’isamoduleinPythonthatisusefulto
maporlinktoabinaryfileandmanipulatethedataofthefileaswe
dowiththestrings.
•Itmeans,onceabinaryfileiscreatedwithsomedata,thatdatais
viewedasstringsandcanbemanipulatedusingmmapmodule.The
firststeptousethemmapmoduleistomapthefileusingthe
mmap()methodas:
mm=mmap.mmap(f.fileno(),0)
•Thiswillmapthecurrentlyopenedfile(i.e.‘f’)withthefileobject
‘mm

•Pleaseobservetheargumentsofmmap()method.
•Thefirstargumentis‘f.fileno()’.
•Thisindicatesthatfileno()isahandletothefileobject‘f’.
•This‘f’representstheactualbinaryfilethatisbeingmapped.Thesecond
argumentiszero(0)representsthetotalsizeofthefileshouldbe
consideredformapping.So,theentirefilerepresentedbythefileobject
‘f’ismappedinmemorytotheobject‘mm’.Thismeans,‘mm’willnow
onwardsbehavelikethefile‘f’.

mmap
•Now, we can read the data from the file using read() or readline()
methods as:
print(mm.read()) #displays entire file data
print(mm.readline()) #displays the first line of the file

mmap
•Wecanretrievedatafromthefileusingslicingoperatoras:
print(mm[5:])#displayfrom5thbytetilltheend
print(mm[5:10])#displayfrom5thto9thbytes
•Itisalsopossibletomodifyorreplacethedataofthefileusingslicing
as:
mm[5:10]=str#replacefrom5thto9thcharactersbystring‘str’

mmap
•Wecanalsousefind()methodthatreturnsthefirstpositionofastringin
thefileas:
n=mm.find(name)
#returnthepositionofnameinthefile
•Wecanalsouseseek()methodtopositionthefilepointertoany
positionwewantas:
mm.seek(10,0)
#positionthefilepointerto10thbytefrombeginningoffile

XIV ZIPPING AND UNZIPPING OF FILES

ZIPPING AND UNZIPPING OF FILES
•Weknowthatsomesoftwareslike‘winzip’providezippingand
unzippingoffiledata.
•Inzippingthefilecontents,followingtwothingscouldhappen:
Thefilecontentsarecompressedandhencethesizewillbereduced.
Theformatofdatawillbechangedmakingitunreadable.

ZIPPING AND UNZIPPING OF FILES
•Whilezippingafilecontent,azippingalgorithm(logic)isusedinsuch
awaythatthealgorithmfirstfindsoutwhichbitpatternismost
oftenrepeatedintheoriginalfileandreplacesthatbitpatternwitha
0.Thenthealgorithmsearchesforthenextbitpatternwhichismost
oftenrepeatedintheinputfile.
•Initsplace,a1issubstituted.Thethirdrepeatedbitpatternwillbe
replacedby10,thefourthby11,thefifthby100,andsoon.Inthis
way,theoriginalbitpatternsarereplacedbylessernumberofbits.
Thisfilewithlessernumberofbitsiscalled‘zippedfile’or
‘compressedfile’.

ZIPPING AND UNZIPPING OF FILES
•In Python, the module zipfilecontains ZipFileclass that helps us to zip
or unzip a file contents. For example, to zip the files, we should first
pass the zip file name in write mode with an attribute ZIP_DEFLATED
to the ZipFileclass object as:
f = ZipFile('test.zip', 'w', ZIP_DEFLATED)
•Here, ‘f’ is the ZipFileclass object to which test.zip file name is
passed. This is the zip file that is created finally. The next step is to
add the filenames that are to be zipped, using write() method as:
f.write('file1.txt’)
f.write('file2.txt')

Python program to compress the contents of files

extractall() method
•Inthepreviousprogram,weassumedthatthethreefiles:file1.txt,file2.txt
andfile3.txtarealreadyavailableinthecurrentdirectorywherethis
programisrun.
•Tounzipthecontentsofthecompressedfilesandgetbacktheiroriginal
contents,wecanuseZipFileclassobjectinreadmodeas:
z=ZipFile('test.zip','r’)
•Here,test.zipisthefilenamethatcontainsthecompressedfiles.
•Toextractallthefilesfromthezipfileobject‘z’,wecanusethe
extractall()methodas:
z.extractall()
.

Python program to unzip the contents of the files
that are available in a zip file