0 ratings0% found this document useful (0 votes) 34 views9 pagesUnit 6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
File Tlo Handling and Exception
Handling
List different modes of opening File in python
[+ Read mode Cpl):
Opens the Mid For recat gott iS _used cohen the
File i Oh) being read. Pia Pile pointer at the
beginning of File. mq
Lalit mode (wt) *
Jal ite paacd ahich (sused to edit and corite mec —
[nPocmction te the i)
2:>Append made Ca")?
Appencdiag mene which is used to acid new data to
the end of the Pile} that isn. information is
Cute matically acljucted ta the end
4+ Read and torite mode ("r+")
pecia) read and rite mod. cohich Ss Used to
handle bath actHans cohen coor king coith Fil
S- Binary read mode ("rh"):
penl a efile For reading ool io binary format
Pla Bile pointer cat the beginning OF the oi)
G2 Binary write mode ( Mw).
Opens the file Fac coriting only in binary Faomat.
ite an
pens the file Por hot
0
ist
@ scanned with OKEN Scanner|. python striag “format Function—t
| age = °
T
rele Een eee
_ t) mrethe d_ toith exampin
explain use of Forme mato) memes — ‘
tool that
vTthe fecmatt) meted is-a—Pose
create Facmctt A Srings
allows deve jopers to—
by embedding
placehel a png _eoitnin Oo —tmplode—SEMOSe so
Tie methac orrens a flexible and vier
| taxcry, go _canetcuck be xr! oureut _f
Re eooqe ooo ee | -
Jnay_ been
values fore
SAbDC
insroduce dis lex Sh
| Farmatting more eferciently
a rrt—~—~—~s—ss—SsS—S— — ? ~
0) Attribute Errer
iD ey Errer ~
— 4. tO Error?
bow t
with open ("“ nenexf stent Pile txt "n") as file!
centent = file. read t)
cent 10 €rrer os €+
| print LE" TO eerer occurred: fei"). =
oes TOErcer occured : Eerrne 23 No such
Cie oF aietctmry | nonexistent = file sbxk’
Attribute Error?
ass FyampieCla: He
ef inir. Cseie , Natue): . eee
Seif viajue* Valu
Obj = F xampleClas s C42)
pri +Cobj_.nene iotent pars ribute)
CO Aaa cl
V “=
@ scanned with OKEN ScannerAvi Chute rres occured xample Class! Obyect
has no attribute ' nonevistent. attribute!
I ' " a Nena" “opel t
my wctict = f “name tt: Yorn age 2s t
| Metie 2 mys click £ "nonexistent. key "7
except keyrrar as _¢
| print Ce" Jey Brean occured : Seg")
CIP i 4 ye cror ocaured 1 Monenistent —Isey"
B-S| Explain the uSe oF keyword "raise" Por Python's
exception handling, Also explain New to create
use r- dejined exception in Pythen.
Thy = offre.
Canditing OF eccucenss
1
is The catse keyicnned is used to expticitiy raise
| exceptings ducing Cu. tei One
He ef. Ajten USed hen aA Certara mali tt
pure that warrants the raising o¢ ci specicic
; : q
exception. . —
|. Ovthon allows You te create .ser=cdeft ed
ceptian to handle | situs tlon einige to your
cacle: i qi
Sx
@ scanned with OKEN Scannerrn | Cgele. ennon
i e
= I wep init_3 ee oe
e ©
io | seit, message + MeSSe95—
= [yoo ey init Coe ames.sag
= ery ¢
— | ra CustomerErrer(" Thi : Cust
= L error message-")
= |
| except CUurtamerror as ce!
l
> print CE" Custer Ereer s foe 3") ao
7: -OlP: Custom Error: This is a Custam ences —
se message!
3
T » 1 —
tT [- 5.0 the bos exacpie,
sThe Custom Error ola P cla ined| Foheritins
—e lero om Eaception. zt hasan eptional mess e
parameter.
————Ststom_os.g_using the nai:
A_fnstance of custem Error is roy ith
Se_heycoorsh
@ scanned with OKEN Scannere The cxcepl block Catches the Custer Error and
| pefois_tne_ctiatam _Lerrar message
pichonary
Pythoes aS. module provides methods that hbejp you _
_—_L_perferm— file = processing Operations, SuchoS
___ renaming—cuncl__clelering_
putt = fn fue? for fr ~ ___
| remove _anel_ rename 0), Te Use _tnese _firn?_cos—
_bave te import os maclule First and theo sou
can cout_any relatecl Pune tions.
_ we have 00 7mporhan
i) rename 1)
| the rename ct) tncrion takes A100 _cicqueremt S_, tne
___Leuerent. etre | directory —_name One + mero. ete }
Lstirectany = Name
Syokeu! 2
Los. rename (curreot = file) clirec Pye Dame Nevo. File)
A" pep piie txt")
[ne temavet) funtion _is to cleiete dineetany by
—PSeeci tying 145 name ac the aegunment____
+
@ scanned with OKEN Scanner| stuclent
L | are cticHionar) ef ey aaa ;
eee i cre: oi CM
| 6-7 wae en thefe ROLLNO
ee | nat, ee ee
F ee ie
= students alist =
i
t= i :
[epudents —clict 41s Charlie
1 oo ee
@ scanned with OKEN Scanner\
aye He
Date
(9: 'Sbreyos', 31 'Bob', 4
| ei" pavicl! 5
\\\\
Jour put £ = —
\\\
@ scanned with OKEN Scanner