0% found this document useful (0 votes)
33 views19 pages

File Handing Class 12

Uploaded by

areebshelby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views19 pages

File Handing Class 12

Uploaded by

areebshelby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

FILE HA1'TILING

\ ~....... - o~- -"-.. - - •


... ... "-1'3~ • ~~ ~rr:..:a..ureli =:--:""...~:io,-~ ~~ ~~
i.axn:-nu:_io~ r~~~u.en~~
.!.._ a!;:: 5 ;; f:Um"OC. ~a.:m_ m:. .l :5:::L~~-- ~~:::~ --.!'-"- \,; X1.: ~.i. .L..~

DA. T _\. FILE


~?fL.::uro:-..
Com:--!-~~ .src:-~ "e\ er.."" 1tL~ ~ ,1 ~ --~~'" ~ ..."'i'.- K .-.~ ~ t-..
Tni~d<l:-c. ~~- .i:e ~ ~-w.lli- ·_,,-: ~ s..-ri~:i" 1;_),; ti.~---!\~""-

P\.moa h.-.,.~~ ~~~m tt~ ':'l"'----s <.,,.-.......:~ ·-~ ~ t ' ~~ ~


...... - - .I. ...
::\tn: • ,"S.. ~~'\ ,...._

TeA°t File
It ofdu:- .-.~ -~...__, ."-; c·, - ~
.i ~:i;:n.~
g-mth.~ h ,m~ ulk---mutt:Mt m !.~ ~~l
, .:'!'Tea.1JU.~~ ,~ !b, -~ -u. .k.;.--::..- :-. :.
\X ~ ( ' ~o . iur. . .,~ ' r-........· I." !;\
p:2-.:-r.
n~c . .
:ut" u~.dh s.: ... , - t\l: "' - .. .
a::c.n.~,~ u,-.:J m : i - - ~... ~ J. ,

<: .o:t
qI

\\ I 'I'''' I I 111, , 11" I" ,III.-,, 11111, I "" I 11 ,, J1111 I ' I r 1Jii11l11
11111 llh 1I II II 11111 I I I II II II 1111, I,' it! ,I I I I
1 IP 1l1• 1 \11 •\\ 1111 1 !ill II iillil\j 11,,i 11\lii\\

cl \ 1111•1 1 I \ lllt l Ii •I Ii 'I 111 111 l11 I\ I 11 "' lilt I 11 ,1111, 1 I I I , , I


111111' r 11d ,1111111111,, ii 1111 1111 , I I Ii 111, 111, ,I,,
111 ,1 r ,1 1 11 , 1, 1l1 ,, 1 111 ~ llh 1111 w111111u
I '11t11 I \ llh I It 11 l II 111 I Ii 11 II I II 111 II I II II I I I l II 111 II I I I I 11\t
1 1
p111111t•1 I·, pli111111 ii 11111 I 11il 1d 1111 llli • II 111 111 I I I
1 1
111111 11111 th111• 111111• I ,I 111 11 11 1111 111 1 w 111 11 1
1, 1o1d11111 ,111il w,1111111
, fik
'l'lw Fllt• Oh,!l'd All l'lhut,·~
l d,• 11\li1•11, ,11 1• 11\t d 111 11•1 1.I ,1 1111 WI 111 11.11,1 lei t Ide 1 lll dt ,I,
l '11, ,, 11 11 ,11·1 lilt 1ilq1, 1, y,,11, 111
1,1•1111l ,111 il 1·1111
,t 111'- 1, 11111
!~1·1 \ ,lll\111\ 111!,11 111 ,1111111 11-l.111 •d 111 il1.11 Iii,
\II ,11111h1111·, 11·l.1t1·d 111 lik 1>l1)l 11 ,111• 1•, l11ll11w•,
J\tt1 ll111h1 1)1111< tlpllPII
1ilt 1 1' l11i,nd 1;111111111, 11111 1 II 1111• llli 11•1 1.li 11,111 l.1i1l1 11 1w1·, 1 1 , ,1 ,,
ORKl~G \Vll~l-1 TEX'J' rtLES 111\ 1 1111 1\11 1
1
I IPl11111', 11111 .11 1.11•, •, 1111111i 1111wl1111, 111, 1 w11•, "I 1 11 1
111

~.v,.1gr.untnin~ Lrn,''l ,,,.. I I\l'l1,.' • h.11-i\ 111,, lhll\\(I Ht'IIIII\ ', lhll\11 1 (II 1111 1 111, 1
• ' ..
·::- .,~ luh.l.: ~.t.,.
.. ._\ ,,-.,. ,II\' I\\\\
1.'p~:n ,;111..l 1..k,~1..-. lid~111,.• \\111lrni:
j- t ,;1 l\ ,ih.ln ..lllf-ll,lf_t', \ t'U lh.'\'\l I\\ ()pt'I\ it lll :\
l'r,1~1':11\1 'l'o n1n-11 th1· ltl1· w,111g 11p1·11() 1111•11111d
f 111w11( .. , 111•. l xi", "w"' l
·:mnJc .1, r~r the l1k· nunipuL1u . .,n t.1~k ,,,\I \\,\!Ht\) p1i11\\"N c11111 1 11 1 111{ 1 lil1 1 : "'. f.11i1111L)
p1·111\("(l11::,1 1 tl il l 11111: ", 1 ., 111 11il)
p1·i11\ ("l)l\\1111111\ llltlll1 1 : ", I ,11111(11)
ning a File Output
·: h.lndling. the tt~t rc1..1ui1~t1h.'l\t i:- t,pcnin!:, .l t,k. N,11'\l' ,11 tlw 1111.· · Ilk l \t
~.if:., orc-neJ. re.1J. "rite..1ppcnd. 1,.h,st·. 1.'t( .. 1.\111 lw ~ 'lo~nl 01 l\lll: l•,1ls1.·
• ~t..i on ihe tile. t )pen in~ m,Hk: "
::,m ,l file. or~n~) ,m·tht)d l:i LlS~1..i in l\tht)ll.
( ,losin~ a Fill'
ect open l ti l P _name[. arces s_nwci~ J) It is 1mpmt.1n1 I() 1.IP,1.' 1,b a, snn11 ,ts you It 1vc l'i11i!.lnd

\'IHI! "01-k with l,lc. ( )p~ 11rd l1k 1s 1. ln~cd liy l ,tlli11g, die
mieu:rs
cl1lSl'() tnl'tlwd 1.\I n, lite uliil'l h,
fik_name The file ,ume .1rgument 1s ,1 srrin~ v.dul' dw
P} dwn .t\llOlll,ll icill) \ lnsr, .1 1,k wltl'I\ t It~ 1l'll'l l'm l' olijr, t
conuim the n.,m~ 1..)f the file to be t)pt•nt•d.
ol ,1 IHe 1s tl',1ssi~111.·d to .tlllll hl·t l1lr. It 1s .1 good pr;tl tic l' to
l'C'Cess mode rhr .h:~-t'~S l\H)dl' 1..ktl'Ltl1il1l'S rhl' mnde 111
llSl' 1h1.· dn~l'o llll'thod 10 , !mt ,I Ilk.
iiuch~he t1'e h.b fl.) h.· opent'd, 1.l'.. re.id, :\ ntt\ .1pp 1:nd,
~c. This is l)ptiorul p,1ramt:tcr .md th1..· def.wit Ide ,1ccess ~,nt.1, I I l11 0ll.lPt I .l lo <i1 1 ( i

mode i, read\r). I\ thun n1.1kl's surl' th.It any unwritt<:n or 11m,1ved d,11:i 1~

llmlwd oil w d1r fill' hdcm· ir is clo-,td.


xt File Access Mode
file a..:~·1..--s.s mode!i .md their dt·$cription arc .is follow~ ProJ!.r,1m To close the file using close() method.
1 opt.ln("rile.lxl"."w"l
Description
P1• int ( "Na me of the r i I e : " . I . n c1 me J
Opens a f, e for reading only. The Ille pointer 1s print("Clo<:ied or not: ". f .closPd,
placed at the beginning of the t11e This 1s the default
print("Openinq mode: ",f.1nocie)
mode
f .rlose, l -
•+ Opens a hie for both reading and writing . The file
oomter olaced at the beginning of the file Output
w Opens a r, e tor writing only Overwrites the ftie 1f the Name of the file: file. ext
f e exists 11 the tile. does not exist, 11 creates a new
Closed ur not: Fals~
fie for wnt1ng.
Openi11g _mnd1'!..:..._uc
91
,.,I•.,!JI ".
' File
• . . . WI Orx' n:l o. ftl r lor lJoll 1w,111nq
,lt
ile th,ir cniH,uns .mlo rm' tti on . I
[11 I l • 111 ;t
1
!he ltlo ti lhe 1110 exi sts II ll1u ;;;r icJ, 19 Overwn:es
i n n1t:nl0[)1• ln hrnarv ft11..•~ 1 l is it
.i..: ·' • 1<.: fnrm ,u crealr s a now l1lr) tor rernl1 ncJ ;mn~,, nnt exisl
•• I111\'J
' ' Ill LL' 111111 • ·
' IJ ind no rr,rns ,ll1ons occu 1
• · -] ' ' • '.
. l 1.: 1s ,11 1.'. used fo r a Opens a !,le for appending l he f,lo no11 ,ter 1, plar.ed
l . 1 r 1l'rc rhl' .
-onlcnt SUL 1 .1~ in 1asic aud' . y repre!l1.:nt the at the end of the Ille tf the file exists If the ti\.-. docc;
~I l • t> ' io, v ideo . , not exist, ,t creates a new Ille for writing
,·· rl1t''t' hk, .u e not human re· d·1bl ' cxccutabl~ files
· . -
• ·· n· t1k IS .d.n. .
a - c Th m
· e 1 e extensr'on
, a+ Opens a file for both appending and reading. lhe tne
jr J}.l, I pointer ,s placed at the end of the file ti the file exists
II the !tie does not exist. ,t creates a new file for
-~\~ Filr read ing and writing
• . ,(l11 nma Sep,1rared Values) c .
,1 ' iormat is on f l The File Object Attributes
l· :.Jnd common ways to stor d . e o t le most
~·• . e ata tn t b I c File obj ects are used to read and write data to a fil e on disk.
rJrubr data in plain text Eacl1 1. fa u ar rorm. It
,' · me o ti1 fil · Once a fil e is opened and yo u have on e fil e object, you can
··-d f,Jch record consists of on e l e 1s a data
.1.u · · T e or more field d gee various information related to char tile.
• mrn:tS- o rep resent a CSV fil1 . s separate
,,1 . e, it must b d ·h All attributes related to file object are as follows
,--S\ file extension. e save wu
Attribute Description
file .closed Returns True if the file ts closed , otherwise False
\\'ORKING WITH TEXT FILES file mode Returns the access mode 1n which file was opened
. .-:,_·ery -?rogramming langhage ' ch ere are• two bas1c . file .name Returns name of the file .
-f!JlClples to han dl e file, open and close B f, ki
t . p h l · e ore wor ng Program To open the file using open() method .
.,....dJ file m yt on anguage , you need to open ·1c ·in a
f=ope n( "file . t xt", "w" )
~-ific mode as per the file manipulation task you wane to
pr i nt ( " Na me of the file: " ,f . name )
:dorm. pr i nt(" Clo sed or not : ", f . c l os ed )
I
pr in t( "Open ing mode : " , f . mode )
Opening a File Output
'.J fJe handling, the first requirement is opening a file . Name of the file : f1le.rxt
)neea file is opened, read, write, append, close, etc., can be Closed or not: False
rrformed on the file. Opening mode: w
foopen a file, open() method is used in Python.

, ntJ, Closing a File


"i 1 e0bjec t= ope n ( fi 1 e_ nam e[ , access _m ode ] ) It is important to close files as soon as yo u have fi nished
your work with file . O pened file is closed by calling the
Puameters
file_name The file_name argument is a sering value that close() method o.f its- file objects, .
contains the name of the file to be opened. Python au·comatically closes a file when the reference object
of a file is reassigned to ano ther fi le. It is a good practice co
access mode The access mode determines the mode in
use rhe close() method to close a file.
which~he file has to be opened, i.e., read, write, append,
ere. This is optional parameter and the default file access Synta, Fil eObject . close( )
mode is read (r). Python makes sure that any unwritten or unsaved data is
flushed off ro the file before it is closed.
Text File Access Mode
~e file access modes and their description are as follows Program To close the file using close() method.
f =o pe n( "fil e . txt ", "w " )
MOde Description
print ( "Nam e of the file: ",f . name)
Opens a file for reading only The file pointer is print ( "Clo sed or not : " ,f.closed J
placed at the beginning of the file . This is the default
print ( "Opening mode : ", f.mode)
mode
f. c lo s e ()
Opens a tile for both reading and wnllng . The file
pointer placed at the beginning of the file. Output
w Opens a file for writing only Overwrites the file if the Name of the file: file.ext
lite exists . If the file does not exist, 1t creates a new Closed or not: False
file for writing
Opening mode: w
I AJl/110110 I ''( )Mt'U'f Hl s, 'IJ,~, I
q, I

11, 1tn11\ ttttu FHP" r I '


I I ,t
: 111 r ,, 11 'I,
i r, ,, 11 I
( I, I :
( "I 11 I I, r
I 11 1 " 1 11 111 \', \o 1 11\,, ", 111 1 11,, ,1 111 11p1 11 it III w 1111 111 r,,tlllf' I 111 ,
',f
\l l'1111 1ll\il,I, II"' "I'' II ,111 '11111111)\ lih "' \' 1111 lllfllll' ,h,
1
I
I , ·,1r f f 1 ' if /I j
I
n 1
/

l'h'"''" ,_\", ,,,I\ I,, ,,,,~,d 111,I d,, Iii, nl,1,,1 will h,
"'"

I , , 1, ', (' 1J
\
1
1•:1.1lll\l\\1I ,,, 1111 11'1\lllllltlt\ ,,I du 111, ,
\ 'n 1h{ 1,1h, 1 h 111,I. 111 ''l'l'' 11d 1110d,, 111 w d.111 will lu '11-111,111
, ( ,•11111loy1·t: ; ">
.,
11( y,,
hl.1, ,I 11 1111' , 11d ,,j d11 p1, \'lull~ d 11,1 Pi !Ii, 1111 11l,p·1 I j,. ,11 J/1111 I /t,1111 1 11
th, , 11,I ,,1 1h, 1,1. • .,. "' ('IIIJll1t/l'I . Y-111,.,111
, ',I I I I'' Jlil" I ,

'" l'ul,,111, th,,, ,111 '"o 1111 tl111d,., 111 Id, 11h1,,, P( wlii, Ii 1111· ,.. uf 1, 1111 ,Joy,•£ ; A•J11,111
l ',jjl/'I JJ:011,
'"' ,I It• \\ 1111 d n,1 11111, ,t Ill,, h111f•J 11,11111 ,,/ ,·1n11l1,yt•t•; Y'.y,1at1

(,1) ,, I ht~() l~11tt t ,,.11111• (It i·1npl11y,·1•: y mH1


b,wr t1,11,11• ,,/ 4•111ploy,·,· : J-laf,1,J
,, ",, n 1111 , """ , d,1 , ,l ,, 1111l•· ,,11d ,.,. 1111·, i, ;11, u i1 w Iti,•, ,.,. 11,
1111 d, 11 .I ,It,,\ li111 .11 hl ,1 o, w 1111 tlu1.14111 ('\n') 10 dw ,•nil 1d ( :,·e:;f i•d J1iJr
1h, •111111:., omploy00 ,tit
~\ Ill.I\ ,,, r,,;a
, 11l1'11.111dl1• , w11l1•t•,1r1i rtws ,ii
A•,hn lt
I It 11 , ,, I I i-. Iii, 11 k11 Ill rd II)' I%·11.,rn 111· ~. , Pild l~111dlt.:,, n,,,,,n
i, thr 11'r 11h}l='d l,oldi.11f, oi,~·u lili•'1, wlt'H'IH , •• V.1.ritn
, lfohiJ!
Prog11111, 'l'u wrt111 II l'Jl1ploy1w'H llfltJll: ill fl Ir.XI
11tt' u-.11,,: Wl Ii c•() IIHII ht HI.
11 il I' 11 l ,, I III fl I () y I 'I' I X I " '
I "w" ) I( numeric d:irn ,ir<: tc, he.: wrim:n ro a text file, thedt.
1t 11111 1 : tl, be ,Alt1vr·rt1:J into string before writing t<i thef?t_
11 r I II I ( "I I l , · d Ii 1•,, 1111 \, r r ,. ,i I , ,<I" ) ~.g. ( apari C "h'211 o. Lit".· t1',
I' I ',I I : Sa I dry 12soo
I 11 I' l l 11 r .J 11 nr• ( 1,) : r .~Jr' I t, C ' ( tr ( <; d I ,:ff j ) )
I

1111111" l11p111 ("I nl /\I' n.i111r• or l 11,1plo,vuc:") prlnl.(''5ucce 1,',fully c0mpili:-d"J


I .WI l I 1•(11,ir111 1 ) f . 1.. I o(, r• < )
I 'l I() 'd' ( ) Output
<hu1,u1 S11ccc1,sfully compiled
h1H·1· 11.nm c,f l·mploy,·,·: Sl11ey,1 Created Pile
F11H·1 11.111w nf 1•111ployet•: l(hw,hi
hello.bet
I· 111<'1 n.111w ol t·inploy('<' : Asl11ni1
J~nll•r 11.,,1w of cmploy,·r: l<iy:inn l 12500
Enttr 11a11w of c111pluy,·c: l(ir·a11
E111t·1 11.1111e· of c111ployc<..· ; R,ill!II (b) writelincs ()
Ui.\•, 1..111 ,, <: tlw <r,•a1ed lik i11 dw sn111e folder whc:rc ~hc:ncvc1, we have to write: a c;equence of string ro l
Python p101~1,1111 i\ <,;JvNI. will me wrirdincc;() imtead of write().
Created Vil,· Sy11t;1x <1 l lr.llandl"/,writeline s(Li
employee txt, 1fore, .ill mings in list L as lines co file refer
-:Pilcl fan<llc.-.

Program To write a teacher's name line byline i


y 011 t .rn .11_.,o hold ',hnu• da1,1 111:,tp;11,1t(· li11,.., 11 .,ing writ<:() text file m;ing Writelines() method
11 wd11HI with tlw lwlp ol 1ww l11w, lwa, tl't '\n' ,11, c, int(iriput("lfow mdny record s do yo-;,
we n:
t• ,r.;, //'•1•n
V ( ""mt1 I <,11 , •• L1. I". "w" 1
i f n, 1 I I : f •ope n("fe,,cher,t,,t" "w"J
pr 1 nl ( ''I I I 1• rl1i"'• 111d, , l'l'd l.<•<1") if not f: •
prinL("fife does not created" !
I Alll11011e I coMPUTER sql:~tr
(,

u Hting iuh) Fih\s (' l ' <' :


frJr
in r rl n CJ i?
( f1 ) :

For "ritm~ tu ,\ i1\1..', "r fiN 11c..-d ,o opl·n it 111 write 01 II


nr
1111
inpul ("rnl<'r nctrn!l
- Of
.an)(n\.\ moJe-. \f \\( "l'C'll II\ C\.i,tinF, Ill..? in\\ me mode, thl r.wriLc(nr1me + '\n')
pm llm, d.n..i ,, ill he- ._: .1,1.1.l ,mJ the ftle object will be
1 f.clcse ()
11X"lthll\N "' ~ of thc file.
lt tht' b~•innt;\''
On the- ,Hhd hJnd. in 1ppcnd mode, ne\v data wiJI be Output Sh
.ktJ~I .:.t the -:-nd ,.lf thc pre-, 1ous data as the file object is at Enter name
f employee : rcya .
O
the cnJ '-'f th . . t"ik. Enter name O
f employee : Khush1.
In PYth,,u, lh1.·re ,m: mo method::. of file objects which are Enrer name O
f employee: Ashmtt
.
u,~t tn w11 h . ,Uta into a files.
Enter nam e O f employee : K1yaan
.
( .J ) \\1n h~ \ ) nrer na me O f employee : Kuan
E
,i.rire{) OhthC\<l takes a string and writes it into the file. This Enter name of em ployee : Rahul
~,et4)od ct~ ~ot add J newline character ('\n ') ro the end of Created File
!' ~~ ,1ring..
employee .txt
~-' lil,l\:
srreya
,F11P~~~~-e\.write(strl\ Khus11i
H~rt.'\ strl :.:,. file reterenced bv <FileHandle>. <FileHandle> Ashrr it
Kiyaan
i~ the file-ohi~-r holding op~n file's reference. Kiran
Rahul
Pr .:.~-.. '" To write a employee's name in a text
file usmg writeQ method.
f<=-0pe11\ .. e:::;::~oJee, txt", "'w") If numeric data are to be written to a text file, thedau
if not r : to be converted into string before writing to the me
nrint(UFile does not created") e.g. f=open("hello.txt",'w')
e1se: Salary=l2500
for i i- range(6) : f.write(str(Salary ))
na~e=~rput(hEnter name of employee :") print ("S ucce ssfu ll y compiled" )
i . .a:~ t e ( name ) f.close()
f.clos2Cl
Output
Output
Successfully compiled
Enter name of employee : Shreya
Created File
Encer name of employee : Khushi
Emer name of employee : Ashmir hello.bet
Enrer name of employee : Kiyaan 1 12500
Emer name of employee : Kiran
Emer name of employee : Rahul (b) writelines ()
U~r c.n see che neared file in the same folder where ~enever, we have to write a sequence of ming roa n
Pyrhon program is s.:i,·ed.
will use writelines() instead of write().
Created File
S)11tax <Fi l e Hand l e >. wr i tel i nes ( L)
employee txt. Here, all strings in list L as lines co file referen
<FileHandle>.
Snrey.i:hu\hiAshn!tKiyaanr.iranRahul

ProgTam To Write a teacher's name line by line ,n


You can also hold some <laca in • \Cpara ce lines us·ing • () text file using Writelines() method.
• ' Wrtte
method WJth the hdp of new lrne character '\n' as
c=int(inp ut("How many record s do you
e.g. f • open l .. emp 1 ovee. tx t .. , ·'w" > wa n:
1f not f :
~=open("Teacher.txt","w")
printt·f1le d~e~ not createo·l if not f:
print("File does not created" )
bJ rez.df(sizeU

--r- · -.2. ~~

~ ~~
,_ ',,.~
- --nt 1i ~
~ =-~ <".:"'"-!'.~.l.L:.~ ..._..
--:::.::=-~ ~ -- ..:.-.~~ - - ~ ~
~ :_:: ::._.- ~ ; ~ ~s
~
~ - -~c;_:g_ ~= - - ..,, ~,c-..::::.-:-.:... - ,....,.-$ .I. - - :.:::r:: ~ ;;;;;::.
-~- -~~~ -~~~

-- -

0---";;- ....I.
~=:x:~ ,= ~ ~-
P: ·;:: z:::;j -=:-- - -~ - ~-= -- .::___ -~~ --=. - - ---=c

-:.._ --=S· -=- o.✓- ~ ff::.;---;:,-~~!::_.-~~=-

(CJ ~,ead;;n-e()
TI::-~
___.., - __ _, c'. ~ . . ~ .. ~ - -;:;-:e:

--=- ::.- --=-= ...f- ""--'--== t i;r::---- e:::::


', ~t1: l )111p111
t L [ l 1
,or i 111 f'clnqe(c) : \\ 111 lllll\ IP ill ili ,1111

11c1mc inpuu " rnte, · llcllllt' \\ 1 il~lllll 1,1 )0111 rnH' ,1t 11 ~11li1111111, lo, di 1·11111 rn1,ly,
1. appcncl(lldffif'! ' \11 ' ) lll ll',l1'1t' I: " ) fll.llllll' 11111 ,l"l'.''1111111 1H·11h 1111 v,1111111, 1n11qu1111v, &
,.w r·i t elines(l) lllf"llllllH'lll 1'\ ,1111i11,11i11m ,111d "lt1111I \l'~1111111 .
'(l'i('{)
I ,l I (b) rcad(Isizcl)
1'1,i, nwchod ,,H·rtl1r, how 111,111y I l1,11.H l1·1, d11 "'"'l\
1'~ir11 ' records do you wrnt . 5 should rl'tllrn .
H'''' 111.1111,
1
. .
unl' of te.1dm : Sic.lhaarath . . Sh Synta, d i I 1' li a11d I c 1
, . 1·1• o1d ( 11 J
~Jlf(f 0 • . . ' " arma
1111
1e of tl'Jcher : N 1sha M"ish . Herc. <. Pikl Lrndk .. is dw Ilk ohJn I l111ldi11~, oi,1·11 lilt-\
fJJle r 1 · • 1a
1111 e ot re,1chcr : Soniya Yerm rcforencc .ind ' n' determines 1111mhrr of d,.11.11 fl• ~
n
f;ifC r ' . a 1

£.~ irr f1Jl1H~ ot_ racher : Ki ran Gupra I( no argunicnt is 1-ptx il1l·d, it 1T,1tl., ,1 1011q1ll'l1 li,w ,111 I
fll rtr 11 •1me of teacher : Ansh Sharma rernrns sering.
Program To read n bytes from text file .
(itJled file
my f i 1 c ope11 ( ' M i 11 cl 11 l. . I x L ' , ' r ' l
Teacher.txt• s my r i I e . r cad ( l !i )
pri11L (s )
Siaraaratra Sha~ma my r i 1e . c l o s c ( )
~1isha 'lisrra
Soniya Verma Output
Kiran Gupta
Welcome co arih
Ansr Sharma
Program To read n bytes and then reading some more
bytes from the last position in Lext file.
myfi l e= op en( 'ari l1anL. t xt ·. 'r ·)
s=myfile.read(lO)
Reading from Files print(s)
Afrer writing information into a file, you must need co read s l =myfile.read( 30)
~lucdara at some point. Python provides mainly four cypes print( s l)
i read methods co read data from file. my f i 1 e . c l os e ( )
Htrr, dJe content offile 'arihant.txt' is as follows Output
Wdcome to Arihant! Welcome co
u·dcome co your one-step solutions for all your srudy, arihanr!
prmice and assessment needs for various competitive & Welcome to your one
·ecruirmenr examinations and school segment.
( c) re adline ()
These methods are as follows
The ~eadlineq method will read from a file line by lint:. for
(a) read() r~adlme(), ~ lme is terminated by '\n' or End of Linc (EOL)
~is method returns a string containing all characters in a (1.e. .new lme
. character) · When •·n<l
'" c 11c
01 I l
c-.1 .1s. rc:ac1ec,
k. readlme() will return an empty sering.
Syntax Syntax <Fi 1e Hand 1e >. read 1i n e ( )
'fileHandle>.read() · b y line.
Program
f- , . a file's first three 1·mes 1me
To read
Here, <FilcHandle> is the file object holding open file's -open( ar1hant.txt','r')
rd'crence · We are using .
. a 11c.1 e namej y " an'h anr.txt " storing str=f.readline()
IQrne comenr. print(str)
str=f.readline()
Program To read entire data from text file. pri11L(str)
~Yfile=-open( 'arihant. txt'. 'r')
Sl~:af.readline()
,=myfile.rec1d()
print(str)
Print(s)
f.close()
my f i 1e . c 1ose ( )
<.;OMPUTUt S~II ~,
1
A.1li11one I ·•,t+ 1
94
Oluplll f' ,einr,ving all l·,O( cl
fjk ;1 1cr · 1.1r;1t
Outrut \,r, o f l(•r,,i
\\.dcom~ l•) Jnh.mt! II . ,111dy. ,Jil(.('',; 172
Wdrom(' w q,ur one i.iq,
I
Ml u11on~
(c,,. .',mpcd1ivc
1 )our
&
I
T(lt,11
.
'jl/.l' II
r I lie file: I 75
pr:teti~ aml .l.\'~"ITil 111 nCl•1.h for ,·.rnot~s o
. To print the size of entire fil e in byt
l'h I Ill
To, end a entire file line, by line.
. , l'nW" 1,llll ,·st11<Jrn,.
I f / L' ' r. J
· · t,
f I pr. • .. r1hi1nt.txt , r ) f,-=orr•n
w ,1l Trltt:: str"'f,read()
vr,lt..readl•nr<> r =1en ( s t r )

1 t 1en , ) <-O >:


J of the fil e , ic;" ,1
rint ("SlZC ..
hi fldl' P "bytes )
print ls,
pnntf q
f .close( J
vf11°.close()
Output Output
Wdcomc to arihant! d Si1..e of the file is
\X'clcome ro your one-srep solutions for all your_ ~ru &y, 175 byres
,: var·1ous compctmve
practice an d asse.ssmem needs 1or
To print the number of line of entire
recruitment examinations and school segment. Program , , , 1

f=open('student . txt , r )
(d) readlioes() s=f.readl ines()
This method wilJ rerurn a list of strings, each separated lcount=lenrs)
by '\n'. .
This method reads all rows and retains the newlines
print("Nurnber of line s in fil e is: ",l (

character that is at the end of every row. f.close()


s,11 ta, <FileHandle,.readlines(J Output .
Number of lines in fiJe 1s: 4
J;rogra1- To read complete file in a list from text file.
myfiie=openr·arihant.t1t', ' r'J
J=myfile.readlines() ~ Use of os module
print(s) Python's os module provides a unified interface to anu
myfile . close() operating system methods. The os module 2utomaliLll
Output the right implementation module when it is first import
Before using these modules, you must have to import
['Welcome co arihant!\n', 'Welcome to your one-seep module.
solutions for all your study, \n', 'practice and assessment
nee.as for various competitive & \n', 'recruitment
examinarions and school segment.']
Progr.: To show the size of a file after removing End
Renan1ing and Deleting Files
of Line (EOL) characters and blank spaces and also The Python ..QS, module enables in l!]_(:tiQ.Os 11~
print the size of entire file. operating systerf\. The OS module provides rhe fu
f•open,'arinant .t1t' . 'r' ) that a~c involved in the .. file processing operadoc
str•·· '' renammg, deleting, ere.
~-o rename() method
t~-=O
This method is used to rename the file. This met~0
wh i 1e: £ i. r:
not return any value.
str•f.readlint() S)ntm,
ts ... ts ➔·l fn(!:tr)
s-~•lenr~~r.st r iµ()J os. rename ( ol d_F i 1eName. new_F i 1eName l

print(":> ze of f ile after removing all EOI. We are using a file namely 'srudenr.rxc' sroring rhe CO
Sol, Rahul Vcrma, First division
charar t~rs t blank ~pac~s : ",s) 502
prirlU"Total $1,e of tt.E> 1ile: ".Ls) • Reeccsh Deshrnukh, First division
f . clUSli:'() 3
So , Aaditya Mishra, Second division
4
S0 , Aradhaya Sinha, Third division
SOS, Manish Sharma.Third division
,ir1dlill~
fih' lf ,

I , 'fo displny the• content nt 1•I 1l . ,111 I ' I •I .


f'l1 1• I• of file. I l ll ll t; ln J' Ap1wndin~ l)nta into a File
f1,1111l ' In ordcr ro .1 ppc11cl ,1 nl'W line in )our CJ1i\ting file, r>u need
,~r • I( rt os 1

to opl'n thr file in :1ppcnd mode, 1,y ~crti11g .!· as rhc mode.
r,·studrnt. t.,t. Wnh file accc\s mode J', open () function fir5t t.hccks if
1

,f O!!. pat h. i Sf l l C ( f ): file cxi\ls or nor. If the file docs not cxi~t. 1hen it crates
~
.1·e11c1mt"( ' sLt1drnt.txt· , •st.uuentl txt,) ,rn empty fi le Jnd opens it. WhcreJ~ if file exists rhcn opens
05
fl•':-tudcntl.txt' · ir. Yo u can also use file access mode 'at' co open file in
C1h•Ope11( fl, ' 1·')
reading as wel l as wri ti ng mode.
print("Content of new fi l e... " ) ~yntax
FileObject = open('t1lename·. ·a· or ·a+-·,
for i in ob: The content of 'computer. ext' fil e
print(i .rs t r ip ())
Computer science
ob . close() Head
e' se: Authors
pr in t ( "Sour ce file doe s no t ex i s t") Proofreaders
Program To append data into text fi]e.
l)utpUC
eoorent of new file ... f = open( "computer.txt","a")
f.writ e( "\n Welcome to our company " )
, 1,}lahul Verma, First division f .writ e ( "\n I hope you will learn more here")
0
~2.Reecesh Deshmukh,Firsc division pr i nt ("A pp end the data
0

503,Aadirya Mishra,Second division f . cl ose()


504.Aradhaya Sinha,Third division Output
505,Manish Sharma,Third division Append the data
Content of file after appending che dara :
remove() method Computer science
This method is used to remove the existing file. This Head
method does not return any value. Authors
S)lltiL\ os. remove ( Fi 1eName) Proofreaders
Welcome to our company
Program To remove the file and print the status about
I hope you will learn more here
the file that it exist or not.
import os
print("Enter the file name which you
want to be deleted:") [Check Point 01 J
1. In which form, data are stored in binaryfile?
f=i nput ( )
if os.path.isfile(f): 2. What isfile?
3. What is the use of access mode?
os.remove(f) 4. What is the d(/Jerence between write() a11d writelines() ?
pr i nt("File successfully deleted")
5. How can you terminate the line?
else: 6. When is os module used in Python program?
pr i nt ( "File does not exist")
Output flush() Method
Enter the file name which you want to be deleted: This method will force out any unsaved data chat exists ·
'111b' program buffer to the actual file.
Typically, chis means that the data will be copied f~
File does not exist
...
P.n·t~;·th~· ii-~~~~ ·;hi~h ·;~-~ ·;;~; -t~ -b~-d~i~~~~i=· ... program buffer to the operating system buffer(fj:;~te: a
Python auromatically flushes the files when c'
'sub.tx1' But you may want co flush the data before cir
F'k
1 ,ucccssfully deleted Syntax Fi leObject.flu sh ()

I
rrcc l 1' , 1
1
II II I ,., I
95

To die.piny the content ol 11lc n


111 offile Oci <.hn11r,1nc i\pp( ndin~ l>«1l,1 111to ,\ hlt.
n•"'r. . . In urd r IU .aprcnJ ii ru.w 1111 in )l ur UJ un ftl >1\1 nct-d
to open the file in .tppend m Kl , by tlll a th<: mode
·r t l ~ t'
Wath file ac~~ mode a . open () fun twn fir • h«.b tf
.rl•' 11c-f\: file ex.ms or not. If the file doo not c:<ll then n tatc:1
t ,arnt. t,. t • ' . ,'Uuentl
1• t .◄ tvt" an empty file .md opc,m 1t. Wheras af file c-a t th..rn opcm
.. • tudcnt 1 t~t · · n. You can also US(' file acc.css mode .it-' to open f,k in
4
lnfl.'r') reading as well .1~ writing mode
tt· ontent of new file_") s,nta,
F 1 1POt>, ec t = o~ert · f 1 r 1 • -i •
,nob:
The content of 'computer.net' file
printli .rstrip())
Computer science
b.close() Head
Authors
~~1nt("Source file does not exist") Proofreaders
t,)clJ'tJ(
Program To append data into text file.
[till of new file ... f = open("co~p~ter.txt-."a-i
l f.write("\n Welcome to our company-)
;oi.Rahul Verma, First division f.write ("\n I hope you will lparn more here-,
:,i.;2 Reeresh Deshmukh,Firsr division print ("Apperd the data .. )
3,Aadicya Mishra,Second division f.close{)
Si)-1J.r.ldhaya Sinha,Third division Output
Y.J~.Manish Sharma,Third division Append the data
Content of file after appending the data :
remove() method Computer science
T1us method is used to remove che existing file. This Head
;;icthod does not return any value. Auc:hors
,tax os. remove( Fil eName) Proofreaders
Welcome co our company
~rn To remove the file and print the status about
I hope you will learn more here
~ file that it exist or not.
;,.;,art os
;"1r.t("Enter the file name which you
want to be deleted:" ) ( Check Point 01)
1. In which form. data are .~tared in hinaryfile?
'= "put{)
f os.path.isfile(f): 2. What is.file?
3. What is the use ofaccess mode?
os.remove(f) 4. What is the difference between write() u11J writclint's()?
print(UFile successfully deleted")
5 How can you terminate the line:>
6. When is os module used in Python program?
prlnt{"file does not exist")
Output flush() Method
truer chc file name which you wane ro be deleted: This method will force out any unsaved d.ua that exists
~ program buffer co the actua\ file. 1

File does not exist Typically. this means chat thl' ,.Llta wi\l be copied fth
i ;·~· r:1~ -~~~~ -~hi~h ·;;;~. ~~;;; ·;~ -h~-<l~i~~~d:.... l
Jrooram
t, buffer ro the op~r,ttinrrt' system butTec(ft', • ,~.,1;t .:, ,,1
Python autom,aically flushes the fi\es when 1'
ivh,LU' Hut you may wane to flush the data before ck
fiJc iUcct.ufully deleted Syntax l"ileQbjecL.fluslH)
/j ,. I , <nvwu , , ,, ,., i
I Jll}/Jf(Jllv 1
r j,

I 11fllJ1lll 1 t I' ,''


1' 111 , 1111,1 111 ~ 111, 11,, ,t,d,1'"'" 111, uu11,~,,,,~11,1 ( 'H'Jlfil
1111 I /11 I
I
11111
t;;

111 1 111 ,,if


I /Ill 1111111
1 I/
I j I I ,, f I I 11 IJ I ',,II ·~ I I I I II I I II

I~ I M• 'l,o,I
I I 1 1 1, t I ti ii 11 I

1, I I I 111 If'"'" I
IJ{''
' I I I,, ,, I I I)' Ii,, ,,,.,' I ,,, ' ',.,,,~.~· ,,,~. j,
-~.

I' iI'I/
I I I I q Ii I I II ~ ,1,1,,I if,,, 11'11 , , IIIIH lfHj ,I
I,,,' II JI I ..

I t I It II I ,,,
tl~ltlff I I'
1 yf,
1
lf,14
I
,.,,,
I' 11 I11, JI' I

111•11 r
', ; ,, ,,
" ' ' lj I t" I' I /,
Hf 1/1 '
'/ 11/ ',.. I
lfjlli/11 1 Ji! JH 1
I
lii ,H• ~ lht ,} Ht t,,,_, . J

111 ,,vul,
'I I J" i\ , tHllfl"' I,,,,,' I OJ/. i.';1·
I 111 tli1 1111 111111111111 ,, 111t ~ 111 111 l110, n·, iii I,, Id,,, 1,111p11, '/I"' I I. I •
{} ,, 1111 •,11,1,
I

1Jf 1
1
tlJ' P' :t,10111, :1
,
f:,: .,
'
,,f 1·;'
111,llt I iltt, II 1111 111, Ill d Id, 1111 ili1il ~I I II/ I '
ojJ
/I j, I' j/11 p11' JIJIJ/I ,1 / l:i' '.l}l:.0 m,•• th.~ W ••e-
Ht,tHh,111 /\;•1 1•w~ 1
Mt·tlr<,du fi/, ,I'; II j, 11 1111 • 1111', /J/ 1 111 ,m,j 2 lljf_" 1 ~ ~ ►
✓1wd,I I,, 1,1k1 n :,t, tlw 11'1 1
tt'iU ;~,· dut,
\\ 1 11 11
) 1111,. "' 1111111 ltl, Ill Wil l,'" I 111, , d,,11 l,y d1/1111l1
do 1
1 1 1 111, ~111111111i ,d I l1lii 1111 •1111, 1 !111 11,,dO n11d1 111l Pr11v;n1111 /Jwf'Y H11 JJ' ;•,1t1,-'11 v11de p<,1fJe• _;; ..,
dw111 ,, ,ii~ ,11111 d,, l,11:l11 1li11K 1,l ,1 lllL ,11,d w111, 'J
IH,111 1;,,, 1111 d pwil H" d;ita (Jf' urfl•m f.1f/,1t1,,:J 1.1··
111 d,,.,/ d w, 1\'ll w, 111 ij d 11i1 ,11 Ji,, 1 wf 11I ,L1h1
1
11 1,d 11,, ;;t,1/J IJ'J',111,11' t:1t,• fr,m, i},•·M,.:.
1

I ii, 1 1 1111,, .111, 1111 1 ,lo , 1111, 111 ,,,,~111,,11 11, tl11 Ill, win t( '
1 1
111111'/f I '!',
\4, J11111,, Ill II 11lilill "I ll I ,I, '''"' I 1111 (, ', 'J!IIJlld.l·(. '.11,
111 ,1,,111 ,,,,, 1,I, ~ I "" [Ill d11,d11 111 p, 11111111 1,111d1,111 JII f l'II~ l I ,, , , IJtl f fl. l ·, f 1 , I I l < I •
1 11
11 I 11l 1 11, ~ lit I Ill, '111 ~I ,,,, tl111d1, 111 Ip }''I I/ Ill lllilll/,.,11l1H1 '
I 'if!' 11 1 l 1 I,,. I [

ti,, ,,,,~111,,11 q/ Ill, ,,,,,,,,, I 11 11d ll111 1t ,,,,,, J ,Ill ,,,,,(1 ;fl/I W)/1(
l

I . r ",,rJ I 1 fl' I ,
I 'j
111 1111 1 11•11,d 1111 ,l1J11 11 Ill du l/11
1,r i 11t I "l! i • I. i r • { .
lt 1H( > Mt tliod 11 11', I . I I I I ' J
I I,, 111, fl,,,,11 1 II,, y1111 ii,, , 1111, 111 p,,~1 11,,1, wldtl11 till /1)t , Jir l rd I "(.!Jr r ~ nt J,<J' 1• 'Jfl
1111 1,1111 ii l11 l1 y11 , l11,111 dtl l•t/\/11lt/llf'i o/ r/11 id, I • ,,,.,.~ I fl'I I
-~'"II I I I, I,, I ! I I I , I J 111 I , ', j 'J 1•,1 11 It I ( J
w l111t , Id, 1 ,1,1,,, 11; ,1,, , J, 11ull1 ,ti''/'' 11 /1/, 1nlr,!i"f,,Hr£11t r,r, i' rr ,.;t
I ,' 1 1 Y l 1'J,
/V, 111•, ,,,, i11, 111l11c· ,I ///4 lh/111! /p 111/11;1111, / I• t' ,1111 /,/f. J/!r, 111//1•11/
1

I IIJlljtllfl I • II Ill I I ,, f • r ,, j(j 1r' 1


( I

11 i ,ti 11r1,itl"~\th ;J'J'iit ,:,11 1d•~: ..


A1,d,,,1 I,( Iii ,I ( J

1'111,,I,, ,d, 11) ' I '.!. :


1, r 111 l ( " I i I , ,J <1, nr t ,, 1- • •"
l',,,,lf1!111 '111 I 1111W 11,, I tl/11111111,11111,,,, 111 fl,, 111/1
<l1t1J1fll
11,,, 11 ,1 I If
If 11, 'I 111• i,
1
f m,t line: C1,1nputcr ~cie-nc.c
Ii ,, I II 11,
. ''"'''"'II, I I I
( lfll(11tp,,•,irioni.,: 17
II I I I II I II I
I ,,, , ,,r f 111, •, 1
,,
r 1111u11 p(lsitio11 data: Head
1 r, 1d I J 111 1 ,
V,d, 11,,~i, iun d.11a: read.ere,
,,, 'ti I I I" I I
11 I I 11111 ,r
,,,, I I I 111 1 I
I I I I If I I I If I I It I 1,,,
Rclativc and Absolute Path
11 ''"' 1 ,)•ti
1 ,, 11, I II I
/\. ,d.iiivr pittl, i.; the parh ,hat is relative co c.~
I I I d11t1 H11y h .11i11n on your c.ompucer. Refarivtp,atb~-
/d I II I I '' I I I l 11, ,1 111,1 I I 1 ·• ~1.111 w,d, ,, Jc.idi11~ forward slash and is inte!frrP'
I

d,l 1'U 1·1''-Llivc of the c.urrent working direcro~ ·


bso lute.path is the h path t h.ir co IH,Hnsth
. ,, . l'. F,,
1,1J_ r drr'-'ctof) t ar you nce<l t 0 . _ t . c.:nt1rc pJth t
. 1,le O d. ,1cccss n · n i111po1·1 ,v,
.11e rhe 1, rne 1recrory ot your _ "· us p.uh will
- .11 0 l com pu tn <l 1 f 01w11 1··co1111111tl' 1·. 1,1 ·· 1
, ;;/11 r Jirecror)' rut you W;\nt to . ,in end\\ id
tr co . d slash . J.cccss
.. . Tl 11·
11
,ceh1.i ]t'1Jina v' torwar s nt ·,lth •St, 1rt•·., l . l'l.'ddli1101)

12 - 1 . rer1ctl itH'l)
,11
sys.stdouL.\ffi t L'( 11)
~etc~vdO .
.1h1'. nierhod returns the current work·mg d.1recto 1 , svs . stdou t . \vritP( le'
,ct\-'• ) c .
0 l ,l ::.y::. . slder1· .w1·il.l> ( "lh t' l' l' b no \,,·,·o r" \

', [l . str == os . getcwd() Output


, ~111fil' Computer sci1.•nce
·· ,rat1tfo display the current worki u .
rf\,~ t os nb directory
l-k.1d
impor .
~,,,
05
.getn,id () There is no error
-r;nt("\~orking fo l de r is 'ts .. . s 1
P
ootput (Check Point 02 )
\forking folder is
1, Jr/,t•11 P,·1!,011 dot'.\' c111to111atin11/)'/l11sl, //,c files:'
C:\Users\Arihant
2, Jl'/,at is the use o(tt'II() 111('1/Jo,t:I
abspath() 3, Wrill' tilt' sy111ar o(sl'eA() 111('(//od.
11.jsrnerhod
1u returns · o t· cl1 e
h a normalised absolutised version 4. Name c111y 11ro I O co1111t·ctiw1s.
arhnarne path . 11 n,tmc
c.J c at means returns the complete pat .
P
d
of the ata 11 e.
WORKING WITH BINARY FILES
Python object handles the bin.1ry fib. Python pn.wides ,\
S1ntll\
· fileObject=o s .p at h. abspath(FileNa me) special module-the pickle module which 1.\111 he used tO
prognnnTo display the complete path name of the store any kind of object in file.

dala file. Binary File Access }Jlodc


import os Binary file access modes ,rnd their description .trl' .1s follows
[,='computer. txt ·
p=os. path . abs path C f Mode Description
)
print("Complete path i s ",p l rb Opens a Ille to, 1tiad1ng only 1n b1n:uy foHnat Tt1c
tile po1nte1 1s ploced flt tile beginning of the file
Output This 1s tile default rnode.
Complete path is 1b t- Opens a tile !or both re,,ding ~nd w11lin1J ,n b111ary
C:\Users\Arihanr\compu ter. rxc torn1at. The file pointPr is placed ill the beg1n111ng ot
tMli1e
Standard File Strea1ns wb
Opens 3 file for wnting only in b1nory formJt
Overwrites the Ille 11 the hie exists If t,le does not
The standard file streams are simply pre-opened Pyrhon Glc exist. creotes .:i new tile for wnting
objects that are auromaticall)' connected to che program's
Opens a file for both writing and reading 1n b1n3ry
''llidard streams when Python scarrs up- There are three wbl tormat Overwrites the tite 1f the file exists . II file
bas1C 1/0 connections : standard input. standard output does not exist. 1t creates a new file tor reod1ny and

and standard error. writing


sys.stdin It stands for standard input. To read a line Opens a file for appending 1n b1nnry formot The '110
ab pointer 1s at the end of the 11\e ,t the hie exists 11 tile
from a user (keyboard/input device), you muse be call
does not exist, 1t creates a new tile tor w, 1t1ng
sys.scdin.readline(). Opens a !tie for both appending and reading in
sys.stdout It stands for standard output. To write a line ab+ binary format The file pointer 1s al the end ot the
to a user (monitor). You must be call sys.srdour.wrire(). file 1f the Ille exists II Ille does not exist , 1t c,eates cJ

rd
sys.nderr It stands for standard error. This file object is
forerror mess,ge only' even ,hough irs ourpUC goes
new tile 1or appending and writing

or same destination as srdout.


I COMPUlT R s,
" . '-II N~
I A11i110ne I.~
trickle . .
,•.g, 1mpor f d tLd • int ( input ( •r
Opl'ning and Clo~ing Binar.' Fiks ,wrnbL'r O , n1., r
() ""rnb(l 1

Pl' lllll~ IHll,11\'


· · ll' \ I ,·,IC
11., C , , \ ,l llH' ,I\ Of '1.'lllll~ I j1•II 111<', tll ~ r r,f
· tl • l J
1
hin.m l,b \ ,\11 t,~, l'JlC'IH>tf ll\' " f'n1() rucrh<lJ. 1 s 1. in ,unge(nurnber_of _<1n tdJ:
~\ Ill.I\ , ul( ' Enter ddta · +5trc
forr = inp +
• fllt-'Oh u : t t'fl 'l ( llll'Ndme[,tH•rpss_mode]) . tl append(rJ •,,.
. 1is- o. en ( . imp. a , w J
1 d t. , b,
\ lt nhhc..,J )vu1 \\ t1rking in file. fl ,, important ro cl~s~
1l
file - p (listl file)
11. 1-m 1.h" 111s J l'r<-·ning b11ury file can be used close . kle dump .
mc.-th\,d like ,l:- tt·, r file. pie :. 0 ta entered in file'' )
print( a
~) nt,t, file . close()
FileObJect.close(l
Output 5
c ~ 11lel•open("b1n.dat··,"wb+")
Enrer rhe nu mber of data:
' print(''Name of tl1e file : ",filel.name)
print("Closed or not: " , filel.closed) Enter data l .· 12
printc·opening mode : ". filel.mode) Enter data 2 : 65
filel.close{)
Enrer data 3 : 45
Output
Enter data 4 : 86
Name of rhe file: bin.dar
Closed or nor: False Enter data 5 : 57
Opening mode: wb+ Data entered in file

Using Pickle Module


u-1110 lo c:,. \'let hod

To save any types of data (object structure), Python This method is used ro load data from a binary file.
provide~ a module called pickle. The pickle module is ~sed Syntax
for serializing and de-serializing any Python obJect import pickle
structure.
Serialization is the process of transforming data or an object
in memory (RAM) to a stream of byres called byte stream.
Serialization process is also called pickling. object=pickle. load(FileO bject)
De-serialization or unpickling is the inverse of pickling Ir is important to know that pickle.load0 method
process where a byte stream is converted back to Python raise EO FError when you reach end of file while r
object. from file.
ln Python, pickle module provides two main merhods e.g. import pickle
dump() and load() for writing and reading in binary files. file= open('imp.dat', 'rb' )
U'!ting ,i 11 n1pf ) vtc· hod listl = pickle. load(file )
file .close()
This method is used to write objects to a file. Before use the
dump() method, you first have ro import the pickle module. print('Showing the pickled data:')
Sy11 t,1 ... cnt = 1
impor t pic kle for item in listl:
print('The data', cnt, · is , i te·
cnt +~ 1
pic kle .dump (ooject_to_pickle, FileObject) Output
Parameters Showing the pickled data:
• pickle is the moduJe which can be used co store any kind The data 1 is: 12
of object in file.
The data 2 is: 65
. object_to_pid<le is eirher a variable, an instance of class
or Hst, tuple, ere. The data 3 is: 45
• fileOhject is the name of file in which you wam ro The data 4 is: 86
wdte. The dara 5 is: 57
<)<)

, u:1~ic File Opl'r,1thll1S on Tt'\l No 111, 1111' ,m 11 1111.~ "/1I<' 1ut111!' I.Y ,rr>I'ur,I', IXt ' J111 ,uw,., 1/,e , 0111, ,11.
•';;';iin;1n Files iu P, ll'.011 < '. oll1:µt
U ,t\\
tl 1.11 ,n11, likl ,1.:.11dun~, 111,t·1tiiw I I ·
jkPjll • I' ' r,•lll\lflg ,111d I .l'.l llllU
·" I 1,., 1 (ill· ,Ill( ,,in ,11, l1k ,Ill , ,1111 • S .
I 1.:. , o, we will
, 1111
tlJll'l,lflllll\ Oil ) Oil ll'\I Ilk
Studum
' I,,,,"''",hr,C . Staff
" t1ltt~ ,1 f ik 1'1rn 1 1111 To insert element or data into an
~1·•1r( ,rl' through data in a file · •
~l.11 , h111g existing text file.
1 ,111 • I I h , ll 1s a very
,1,, II 1lll'III lO IC,ll t Houg a file ig110 · def in5erL( J:
"
1
,,,, 11· . . ' ring most of
. ,o ind 011 ly proo:ss1ng l111c~ which meet pa, rt1cu •. l import pick le
ti 111, • ar ' d. )
filel = open( ' col l ege . txt' ,
,-,rfl,I• 'I
, , · u1ill'I, rt Jue 11amely 'school.txt 'storing th e content. whi le True :
11 ,r,t c • ,
y = inp ut( " Enter some th ing : " l
..~o0I p; ckl e . dum p ( y . f i 1e 1 l
(Jc hco ans=i nput("W ant t o ent er more data Y/ M" )
_.,1,lcJJI~ i f a ns . upp e r ( ) == · N• :
brea k
,oor1' fil el. clo se()
~r~r,"11To search any string in text file. in ser t( J
de f che ck (): Output
datafil e= op e n ( 's chool .txt') Enter something: 'Subject'
1ound input ( "Enter any s tring to be Wane to enter more data Y / N Y
se or cll ed: ") Enter something: 'Study'
f == false Wane to enter more data Y / N N
for lin e in datafile:
if round in line: Delete Data from File
f = True Deleting the data from an already existing file, can be
break happen in two ways are as follows:
return f Type 1 For small file, which can fit into memory, i.e. a few
<•.::he ck( ) Mb's.
•f( f •= f rue):
Now, we are using afile namely 'company.txt' storing the content.
print("True")
Company
~1 se:
Department
print("False")
Output Manager
tnrer any .ming to be searched:
Employee
'xhool' Project
rue Program To delete the second word from a text file.
Enitr any ming to be searched: def delete():
faculty' 1-1ith open('company.txt·. 'r') as f:
~~ l = f.readlines()
f.close()
lnsening Data into File print ( l)
li;rning data in already existing file can be happen in rwo del l [1]
'•}l: print("After deletion", l)
f=open('company.txt'. ·w')
h1<:rtion at the end of file or insertion in the middle of th e
f.writelines(l)
~· But there is no way co insert data in the middle of file in f.close()
Ython.
delete()
I() ()
I All/11one I coMPUTEn sc..:11.
~(:,
. ~I;
(. )ucp u c
At., ,c<·d Fil e
( •.
[ l...-.imp.mv\n', ' l )ql.lt1111l111\11', algo1 txt-
'.\ l.111.ii_:l·r\11 •• • I ,n ,1 11 ,, ·\,, • , fl
1 . 'J
' • •' , I 0/l'\ I Get th~ ddtd Vdl~e to b e ae
\ftr-1 dt•lni,,n · I .
2 _ Open trie f ile for r~adirig f 1!!ted
f'l 'omn.in\·\n' '\ f · t '/
• \ • ·1·• r 11 p Ioycl•\n •, · P ro1n Read the completP fi le 4 ~ ro,,
• '1 o 1t
1;
r • 111,tgc r 11 • 3·
4 . Delete the data fron the 0 l!s,
''1>t•~

·1 ," liid1 wi ll not ftt in co m em ory o f·
r•or u,11· 1~ ht·, s. close the file •St ,

1 (:). Operi same file for w•i~!ng i


mp111r-r. Fm thr!l \\L wi ll n eed rw o file, .
'-' s. Close the file 11 to I;

ll;-rr, trr ,,rr u,i11g ,1 file nr1mdy python.txt ' stormg the
ro111r11r
1 Updating a File
• (. ''- c rhc J,H,l v,tluc t o h e d ckt cd .
Updating a fil e in Pyrhon can be h appen in n-ia
.?. (. >p'- 11 d1L· file fo r rL·a Jing fro m it.
3 - Rc.'.id rhe co mpk·tc fil e into a lil>t. we will discuss about the replace a ParticuJa;Y l\ia,
another word. \vorq
•1. DdnL' the d a ta fro m the for.
Lrere we are using a file namely 'server. lou-'
a
.
sto ~~~
~- llo, c rh e fi le . 17,,
I. Ger rhe data value to be inserted With. eco
6. Open !sam e fil e for wriring inro ir. tts Po ..
7 2. Open the original fil e in reading rnode. s,,,
· W' rit <.' th e modified list into Ale.
8 C lo,c the fil e. 3. Open another (temporary) fil e for wricin . .
4. Srarr .
rea d mg . . I
ongrna fil e g tn,1·
Pro•rr•u11 T, d J
n • o e ete the line(s) having word(passed as simultaneously writing it in the ternpo sequen
argument). . . rary fue
impc, · t os 5. This is to be repeated nll you reach the . :
. .
rnsernon o f new d ata va 1ue. pos1~
def filede1(word) :
filel = open( " pyt ho n.l xt .. ,"r" ) 6. Write the new value in temporary fi le.
nfile = ope n ( " a l go l.t x t", "w" ) 7. Repeat step 4 for remaining d a ta of original file
wll'ile True : 8. Delete original file.
line= f i le l.r e adlin e()
9. Change the name of temporary fil e to original :
if not li ne :
b r ea k Prog ram To replace a word with another word.
import o s
else :
import sy s
if wor d i n lin e : import fil e input
p ass f =O
else : print("T e xt to searc h for : ")
prinU li ne) textTo Sear c h = inpu t( " > " )
nfi l e .w r iL e( line) prinL("Te x t to r e pl ace i t with:")
ti le1 . close( J
textToRepla c e = input(" >")
nfi1e.c1ose()
t 1 I l'd e 1 C • ~J, i t e · )
temp Fi le ""' op e n( " se rv er . l og" , 'r+'J
for· line in fil c inpu t.i n put("server. log"
utput
if textTo Sear c h in l ine:
I. Cer rhe dara value to be deleted. temp~ i I e . writ e (l i n e . rep 1ace(te xtrol·
2 . Open the file for reading from it. r c h, Lex ttoR e pl ace)
.3. Read die complclc fik: i11ro a fol. pr•int("Match i s f o und and
4. Dclcrc Lhc dara from rhc Ji~t. modification don e s u ccessful l Y")
f=l
5. Clo~c the fik.
break
6. Op(:n s:imc- file for writing into i1. it c r--o):
8. Close the file. printc"Match is not foun d " )
tempFile.close(J
\()\

( ' 1t·,11t d hll·

sample txt.
The n-xt fow la1•ons will involve downloddlng

,1 11eb p,1KP f, om the Int t•rn11t ~nd reorg.;n I z in~

the contentl int~ u&~ful chun~s of information.

Vou ~,11 I br: doing rrcst of 1 our ~:ork 1,sing Python

code 11rittcn .ino e)(ecuterl 1n V.orro1o Ed!t.

doing into

into doing

oi)
[check P~int ~
\'rl', i,•e are using a file nmnely 'smnple.txt' storing the
1, What is tlu· another 1w111e o/p1ckling!
.,1rnr. 2. pickle! module deals with which type olfiles'
1

~e
111 next few lessonsdwill involve. . downlo"'..l1·1ng a weo,. page 3. Why is need to close 1/"1 /ile a/ier pickling!
c.,ni
11, rhe lncernec
• c an
. reorgan1s111g the contci1 l s, ·111to use 1u1 4. flow many ways are !here/or deletion dawfi'om/i/e'I
chunks of 1111ormat1on. 5. When does pickle load{) method would rct1se EOFError!
\'nu will be going m~sr of your work using Python code
.. ritten and ex~cutcd tn Komodo Edit. WORKING WITH CSV FILES
going into csv files arc used to store a large number of variables or daca.
They arc incredibly simplified spreadsheets. Each line in a
in10 going
csv file is a data record.
Program To replace a more than one same word with
another word. hnpmt CSV Module
'rom tempfile import mk ste mp csv module is a built-in function chat allows Python to parse
from shutil import move these types of files. The csv module implements classes to
from os import remove. c lo se read and write tabular data in csv format. The cs-v module's
def replaceUile_path, pdLtern, subst): reader and writer objects read and write the st:quences
fcQ respectively.
#Create temp file To pull information from csv files, we use loop and split
fh, abs_path = rnksternp() methods to get data from individual columns.
wiU1 open(abs_paLh, 'w') us new_ ! i l e: The csv module explicitly exists to handle chis cask, making
with open(file_.path) as old_fi l e: it much easier to deal with csv formatted files. csv module
for line in old_file: includes all the necessary built-in functions. They arc
new_ f i l e . wr i t e ( l i ne . rep 1 ace csv.n.:ader csv.writer
(pdLLcrn, sub slll c,sv. rcgisttr_dialcc..t csv. unregister_d ialccc
I

close(fh) csv.get dialect • csv.lisl_dialcccs


!/Remove original file • u;v.ficld si·,,cJimit
remove( f i le_pdlh)
//Move new file Open a CSV File
move(ab s_path. file pdth) To work with csv file, chc first thing co do is to open it. Yo
re P1ace(''sample.txL". •·going,
•· "doing") can open a !tic using open() method with its nsmc. It i
Prinl("Reµlace successfu lly") same as n text Hie.
Synta.\ f open ( r i 1eNamr, t1ccr.ss_mode)
Output
Replace successfully
f!aodJing
lOl
~le

Crc.m:d Fil<·
tl',p1.1'
I r
,o <,"'Jrc 1 ror:
fl-~ ~· sample.txt
• r.-l'f' .
,, replace it . h:
()f tO
wit The """t few lessons "111 involv" do-,mload1n&

cflttrcd' a 11eb page frOG the Internet and r~orgar.lzing


#h !) found
1,J!V' . . d <rnc: succc.,!.fu II
• rnodrftcacion
and
,.,ptic1ng chc updJcc file 1, as ro
r II Y the co"tents int? useful chunks of irforma:ion.
-ttf i~ OW5
You will be doing rr.ost of you'" work usi"g Pytnon
~l~ file
~
code 11ri tten and executed in Korro1o Edit.

doing into

into doing

( Check Point 03 J
, u,it art using a file -., 'samp Le.txt' storing the
namelv
1. What is the another name ofpickling'!
t. 2. pickle module deals with which type offifes?
e1c few lessons will involve downlo ad.mg a we b page 3. Why is need to close the.file after pickli11g?
11
che Inrerner
. c an. d reorganising the contents .into use1u
c l 4. How many ways are therefor deletion data from file'!
5. When does pickle.load() method would raise EOFError?
ks of 1nrormauon.
:i will be going m~st of your work using Python code

m:n and executed m Komodo Edit.


WORKING WITH CSV FILES
csv files are used to store a large number of variables or data.
They are incredibly simplified spreadsheets. Each line in a
csv file is a data record.
m To replace a more than one same word with
other word.
Import CSV Module
l'ro ~empf i 1e ; rport mks temp
csv module is a built-in function that allows Python co parse
these types of files. The csv module implements classes to
l'ra~ shutil import mo¥'e
read and write tabular data in csv format. The csv module's
:r'JJ os import remo1e. close
reader and writer objects read and write the sequences
':lef reolace(file_path, pdttern, subst):
respectively.
f=O To pull information from csv files, we use loop and split
llCreate temp file methods co get data from individual columns.
fh, abs_path = mks temp()
with open(abs_path, •w') as new_file: The csv module explicitly exists to handle this cask, making
with open(file_path) as old_file: it much easier to deal with csv formatted files. csv module
includes all che necessary built-in functions. They are
for line in old_file:
new file.write(line.replace csv.reader csv.wricer
-
.
(pattern, subst)) csv. register_dialect
csv.gec_dialect
csv. unregiscer_dialecr
csv.lisc_dialects
close(fh) csv .field_size_limi r
llRemove original file
remove(file_path) Open a CSV File
#Move new fi~e To work with csv file, rhe first rhing co do is to open ic. You
~ove(abs_path. file_pathJ can open a file using open() method with its n•me. It is
'evlace("sample. t.r.t'', "going", "doing")
same as a text file.
r,rjrt("Replace successfully'') Synta~ f=open(FileName.access_model
t
successfully
'( , J I JUI, 11111W I I PMl'll II II ~t'h '-1 I !

It.\ J lf1 \t I I fill l\\11tf1


I' '"' '" ' I 111 I I ll \l1 "Ill I I 11:111111 'I 1 I
1 ft I II ifilft I Ill ltf I f\ l 111 11' I
1,,11, ,11,, I ftlt ' I II I I \;ft I II I ' I dlllll ' I 1l , ·, Ii I i Ii, I
1 II I II I I!I I I I
, ,,,, t
I \ II 1h [ ' I II fl I I l, II II II I I ' ' I '111111
I I I

I I
( fl I II i I 1
1111 , ' • I·
J 1'1111 t 1111 I '!lilt) ' II I ll11
I' I ll \ 'Jh l, 111 11 I ,11 1•111 '
\ II ' lt1 t: l111l
Ii I I I 11111 11 11 •1•1 ltl I"
1111 I ril
II

W1lh· 1111 0 u I '


( ~v11 111\' l li.lur·, l ,,
,,,,,,,.,,]
,, ·111 ,, I''" ,,,, 111,, I 111 1I 1 Il l I l( , ~ l '"I'• 1 ,,,,,111111, ~, 11,, ,\ If th \
111
I 1 , 1 "' ''"lllr 111111 llli I I I II 11, 1, 1
/ 111 " 'l' llt lll I '
(w 1, 1 111 1,l l11q h,,
I .
r1
111, d1, 111,1 r I I ii 111 1H1 1111 •w 1, ,
tl 11111 Ptt,,ltln , 11 ·~
I
11 • 11 \((I1 111 1•1
1'1 11 1 1~
111, ,,., w,1111111 IIfi ll 111 I

Ar I 1 I II " fi ll , 111 •11 1~ 111 t \\ ljli\' lt q, (I


( 'I,, II I '''" Ii,, I/,,,, I ,t/1111 I II,, lilt I
1111 , 1111 1111 1· ti n 111 1,,. lt•1111111 ,11 ,11.1,t l't llt
l1111 , 11111
1•••11il11 1 1111111 r 1111 l•l l/11 11111 I/ II 1, 11111 1•, l 11 '1 ' ,
1 , I II lllr t11 '" 111 ii ll I I 11 1 ''' 11 P
111111111 I IV
,, ,,, I'/''"' " I

• •l•• . 111
I

1,i11•11 I l .v' . "w· >


l , 1·1111, i1 1. t:
,, ,,, ' W1111 ',, II I 1111 /,,, I 111/1111 1 111/1111 11111
ti
WI
WI' I I ('I 'I
I ,V . WI ' I l'I
(I l
w,111111 1 11,r, IIIP ,,,,11,111, 1>1 ,, 11 11 ti 11 111
I 11 '(/II 11 ,,, 1\1 I,I I/,,, ''" w1 1/1·1 ,w 1• il1·1·11wll ' ,\11t1lv •.1 ' ,'Au·
I,,,,,, ( 11111!11 1111 Nl/11
Mt·l11 ·,1•• • ,(1 0Pn•. ,
( lo ~,~ , 1 ( ~V I 'Ill' wi , 11, , ,wi·l l 1•1 •pw(l 'l1· •.tt'1 ', ' 1,-ih,n
l,l10 l ' lll tl •, ' i'll ll tll) ' , ,
Wli, 1, y1,1J 111 /11,1 d ,, ,I w 11l1 w, ,,I, Ir, 1,11~, ir 11111 ~1 111 I 111 •,1'
WI ll t'l',W l 'ilt•J ' t>W(I 'l'l'l1 1C t t ~1 111· llrr•
ti,( lilt I / 1( iJ 1 I\ ,,,,11,,l,I, I u 1 111~1 ,, Id,, d1 1•.1 (} 111t d11id
I II ( tf, I Mith I \(':\l'll(I.' lt,O (JJ ''
I I'

1,11 , , 1 r li,1111, , I 1, ,,r J


!J I l 111 (" ll i1 l i1 1,(ICll' .•,111! ly ddll" )
I ' l I (l',l'()
lk« 1d '"'Ill t. ~v I He ff t. 111,: C'IN, l'l',l(h•,o 0111p111
1,, ,, ,ti d, 1, 111,111 1w /,I,.,., y1 ,11 1111, ~1 11 ,1 iii(' l t'lldtr() I ) ;11 ,t Ml( '( l'/.1i l1tll y .1dd
f11t1t 1111/1 11, t:• '" ,,,, , 11 II ,,,1, I"''/' I , ,.,,, ~ ltrtll /1(111 ll 'lllfll', ,I C:n,:1kd 1%·
,, ,ti, r 1,l,1,,, w/11, I, ,ii ,111 i11 ,.,, ,,, ,,/ l11 w~ 11, d1t , w lilt• Wt·
,,, tl.1 1 /, ,, f,,,,,, 111d1 1111l,1y l11 u •, u, ll w Ill, 111\' ldt \l101dd emp.csv
I, ,,,,£1Hd ,,, ',' 111 1,dl,
,,, ' ( -II, ( ,,t}, , I ✓ I i / t /1,11,11 / ,, • )
1 IU t o1• ,H.1ht1l , l, ,irma,.'8~110,0dhi
/ Ir,,, 11 1" ,or 1111111, 11 /1/r 11r11111-fi, 1/t'llt:11,1111111 /'1 11 ' 1/111111,1: 1/1r
f1111/r 111
Pr•Pjec t Mciri.tt:f•t·, M,1hi se~enJ, 1til',,,, ,NoiJ,1
M ,,, IJ•rf, I 'Y ""' ',Ii 11111 .1, rn{J()() , I)( I/ii
A,, ,ly ,. K, ,,, l.;,,,, I , : ', (}(/(} . , ,, IIJJ
1
1
/ 1111 111111111 ,, \ l,r ty11 C.11,1 1111,,, ~',IHHJ, 11111,t
I>J:;\, A I,,,,,, <,,,,,,.,, !Ll>IHJ , H,1111:/111 (
{ ,~ /I ,f I I I Check Point 04
1
,;1 t '1f1ff, 't11•,1,r.,,11,,r,,( ,; 1 0 1, I :
1 ll '/1111 ,., th, · /ti/I /111111 (I/ ( 'SI '
rr 11/1 r , ',I, rt ,,du , ( I I
! 1 ,T I lfl rt ,1111 f :
I /low fo 11p1·11" <'SI ' Ji/1•''
r,, ,, , , i 1 J. H'm,, 11h1111/ \' II/ode P/ ( 'SI /i/c·.
t , ( I 1, ,r I I "· H'/111'11 /1111, ·11,111 is 11,·,•<ljo, 11 ri1111~ in ,·,l'Ji/(, 1
5. I loll' 1111 1111/ i11/om111{(011 /n,111 ( SI iii,·, '
I All/ltOne I c< >MPLITl~H \(·11 ~c I
1,

rsv I ill~ J--\(:4.. l''·' i\tod«· 'Ki),1,1 11 \l1.1m1a ',, HHJ()(J' '!) ,
J he-I°\·
lnll, '",
,lh ,fin, 1, 11( t,1,· ·" l l , , 1111i,lc, !Cl op('II thl' lw nii- ,I\
()111p111 • ,
1
f 'f\t.,11.,w .
, ' Kirit !',111li,1, /J
. . '"OrJ<J' •
, I >t11i;·,
~,, i 1
f'J\111lr1, '"l •
' , .' ',S ha:ya 1 1.1m1,1 , 31()()() ,
Chnr.lct~r /'Progr,11nmc1 • ' '~200( ' , I\,
Medo
Description ['DB/\', 'Ashmit C11pl,l ' ) B,111glr11 e1 't, I
Rl d fl ,~ dC'fdul' •node ar,J opens a file ror
. a csv File Using csv w .
.
\\
reJding only wnte rn 10 . · r1lt
W-1110 . f
Opers a fife for wnt,ng only Townrcan existing file, you ) fi tr\t o1, t11 htr1~,
rnus1
Overwrites the hie 11 !he file exists Ir . . modes (w, a or r+ 1rs1. hn 1 tr~
Ille does not exist 1I creates a new ~f w~iun~ sed in Python for csv files. tI111, ,
hie for writing funcnon 1s u . . 11
1

Append
Opens a file ror appending The file Tl11s. ru
c. ncrion writes 1tems 111 a scqucnq, (I·
. h b h 11
pointer 1s at the end of the file 11 11 . ) separating r em y comma c aracter · 1 ,
smng
exists. If file does not exist, 11 creates
a new file e.g. import csv , , ,
,~ Read 1- Write
with open ( ·emp. csv . w ) as f:
Opens a file for both reading and
writer= csv.writer(f)
writing The file pointer placed at the
beginning of the file . writer.write row ( [ ·Anal yst' , · Ad t,
X 1
Create Creates a new file Mehr a·. · 300 00 ., Y:~
Close a CSV File writer.writ erow(['Tester·, 'Rahu ] •,
Sharma', · 28000, ,
\\ hc.:n you are finished with work in files, it must to close .
writer.wr1terow ( [ , p roJe .
ct Manager• De
·
che file as soon as possible. To close a file, close() method
i!I used. 'Mahi Sexena'. ' 360oo· ' N
print("Data successfully add") , o•
~)nt.1x FileName.close()
f.close()
Read from CSV File Using csv.reader() Output
To read data from csv files, you muse use rhe reader() Data successfully add
funcrion ro generate a reader object. This function returns a Created File
reader object which is an iterator oflines in the csv file. We
emp.csv
can use a for loop ro display lines in the file. The file should
be opened in 'r' mode.
Analyst,Aditya Mehra,30000,Pune
S_rn tax c r:.. v . read e r ( <Fi 1e Hand 1e>)
Tester,Rahul Sharma,28000,Delhi
Here, we are using a file namely 'designation.csv' storing the
content.
Project Manager, Mahi Sexena, 36000, tloida
Manager, Kiyaan Sharma, 38000, Delhi
AnaJysr, Kirri Sinha, 35000, Delhi
Programmer, Shreya Sharma, 35000, Pune
DBA, Ashmir Gupra, 32000, Banglore
e.g. ;mport cs v @heck Point 04J
wHh open( ' designation .csv') as f:
t. What is thefullform o[CSV?
render csv.reader(f) 2
· How lo open a CSV fiie?
for i in reader:
prfnt(i) 3. Write about X mode ofCSVfile.
4
f.clo<;e() · Which function is used for writing in csv file ?
5
· How to pull information from CSVfiles?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy