You are on page 1of 14

4/5 200

it
ata handling

ransiut Jho w Yn a program


i t takas sGmL wput and pr9Ceduee-
Some Sutput d u t s h n i t mds
the 0Lutlen , the aata uILappeaiw
i.e. it t a broperty
Llmd th isyttern cat
us tempeAu.

hiv happens lucas thu Ldata ntncd


by at tme in primaiy
ru
mO memary, RAM, Ohich is ylahlk
natiu.

Pnaistnt Th program w ype un


pythen aud ie LSL t uw haud
osk w piiislut un atue
This proo prouss keejps Ldala o a
,o
Long m and,en uteh whm
Putant kt doumh.
EN ApeHalung Lsstem
Bus hat is
Ldecumuut t u Ldla
staiud n pumaneut taLaqL deiee
whuch Oan l Tead wRittn
Teurittun uado to tu reauiAL MUut,
OR
tus a stHcam L6equne m
on
chanaetw ]olat ccupiug hamed plee
lsk
Fil IO Tnput Cutpt EOL hauactu is t u cnanactu ( 1
i l I|O mius e a n b Léleud n wmau Tadabl
alata any ligct edi tou
Gm tansk
te LeGndavy itouage had disk) u d Ca be ieLatud
man mumS txt
T
pro ram ihke
Lem haud
h disk
unto RAL ou Lce cuHn u aLr
us
ud wa uw inay fi
Lice usa. ns Binoy Lile a u wsd to sLoL
binaty olata h o d idumiter
u
Programm n Haund c e m a enaractur) {oa u w leu. dlso,
RAM disk nD Lehauast
in binauy ilus
METHODS OF lRITE GHA RACTER DATA IN
Cata StaLLd peumneutty eu ILE
Lcendauy wta agg mudia
Lil lsD.
u usL to Stauu dala such t ) uncke uaki a Lwng las
latu. panametin) Oud wut n w p
F e LELGLUug oaa wtt tud Uu
enanactu ta to add
chanaetu to Lu Cnd o u Siug
NOTE A +o wwut
inayt ab to be a stiug
nwmuice vatuu usLhav to Cenu
at uwct Hiu? ut to a

ehanaciu ww whic uLs a pcgram u pythsn to wid


wtuminatdby, aspuaciat tTim u ogrea nialn to a uct
Chanactu tud EOL Ena 01 Zm Hi "a. txt
w witing ut des Ns acld
to a ict til avtg dny EOL
chanactn to Ltu -tud esning
E pen "a. tet"w") To rEsalu
F. wit u s problum.
Tmu
F clss l) agreat ual. \h) wd "\n' aktua thu n d each list
utum 8tuing
pTuunt " Lata wwitteu utd the i "
KiAult Cus Wuiti a Proram b in Pythen
Pyten
to wut isi uttmss to ct
Kata wuttn i l b.tet
Lnto tue
Cuus wMa Mg ilug listIems a lict fiu
wiu haLppen uk w
PTomm tim'2 t
pen lb.tact ', w)
s Jn the abeu poram, t u data Ftrtet
PTAAut ut filu Hiu Lahall be L L (omputn sciune ln", " Maths \n'j
LGLwAwuttn lim Physics ln"J.
PTOoum E wullnw
F. uclss
w medL spns PTLint i a t ums witun Sucesshutty)
ns eeist, ut mall
a nuw i l Ko wiulug But
Uututi thi hiu i
RLaLLlt
ü t Lalduady. ist ims wittuu
LNistue t u Leentni o t u fia duetrssfully.
tuasLd uaud blauk fi i puud APPENDIN G To A FILE
KOL ulung
Appendmeans o add te at thi end"i.e
) wutulius ) To acdd mo data to a l LSieh
wL Lwse uutt l) a ilung a valr ead has oemL dala Un i t .
at a tumi. BUt it can't
o wutung a liah lptus 3m dhis c a s t l iu b peud
tc. wn a il. w 'a mpd wmcn means
GOOn 'RITE 700D 7RITE
aliady Leis ading em a dlve fites
ile deu LAt
thu
LOist ,it wsiLL heueuca tud.
MtHed u s L to d chanactu data fAm
t data w i l he wuttu ut La uuct ilu
i l be wmtl nc oLthu
Ri. RAad te
TAad the entiu dala
t h fle stauts r a d i 4 uem h i C NO
t appndiug to a n alHuad to t h md o t u filu
p
eeisig R a d ln) = to read u n'chanaeli hom
du uitt pen
du
F
- Juct ):
l' My.t", ")
uth wtant
readingftem tne
F.wut L My name is Chsnta.\n)
uwsep.
F i t e " Tstucly in class 12) Readiinus l) ad Bnly L Uu yem
F.elssz(] h ilu stauts aling Lem t u
ewsa up to a n d indludingz tlue end
append - teoat lU: lus chauaoliw,
F p u "Myl", a )
=

F.wt RAadlnusO -
F. elose)
I tudy empilu.\n"). te riad allanw o m u
l Lstants readEiug e m h e kiu
stauts eadnna e ti uLnsO
lut ead- ket ) :
F CaMy. tet ")
up to t eud
uu F . rdd uilng to doct fil
pTnt luins d kc-fitt sute ():
F. olkU F = open (" a.d et', "w")
E . u t Tme is a great haln.\n"
wAuts- t c t U E wit lkap gsiug \n
")
ppud-tect) F.close)
ead - uet
prit " Lda u i t n inte he fiu)
R!
**
Readting ftem a luoct ils wsin Kiadliu ) wil ruHwm a liu rad ) as
TAad sting emthu filu
d ct-read
=
open L' a. tet ", "r") Jst caL totlu
SF. read ) Ust l u
huetow retuu
Prunt ts) icend Call to Lhu nche14
wuL Yitun dicend liiu
F.CloseC) and so eu,
tuct
witt e ading an tuluL
het- Lad l) tll, we wll h a
to a lutp.
RasuLt
ata ultu int
Tim is a r a t ualnFl
9ptn My. tet ", "o")
Kup g6ung. F. wit My name is hesda . \n
xtt Reading E. wit I ° read un tisw 1 . Ih")
rLady
F. wut I u d sudy vcempulev \
Fclss )
ct-rLad ) :
F =cpen (" q. et "r)
S ou re0d- ttoct )
F.adtid)
Pruut ) F pen l" M. tet")
uu - F. Tadliw ()
F. cLeseO
w l Uu
t i e t - r t a d l). prut ind
n E . rAadlin
F.close
Raout
Tme a

LadJict )
Reauwlt
M homL LLs Ch4shka

L rAOd u class
O00 72ITE
us& a
I etudy
cemputens cnanact olata eceuping Mamed placu
aisk
RAOdlinus ) can he usud to r a d tHe wle neud Hi because oala u stakuo
ntiu Centut o Ltu file pumanemly n ALCMdans mccdua
tilLrctuu cLlist o stungs usiug i l I ) . we may
ach
Cpanaud byln otala Loulen ua.

Assignment -1
tus 3 h e n e blw let and Binany l s
1. What is a file? Why do we need a
file?
2. How many types of files
3.
there?
are
T e jils Binauy fil
Differentiate between text files and binary files.
4 How are binary files faster than text files?
S.What extension is given to a text file?
)ut censLat a0. &tauunee dt ussd
6 What cxtension is given to a binary file? chanactn un Uhucn binany olata
Write a program in Python to create
7 about yourself in it.
a text file "person.txt" and write two sentences each l i w u tminalu
8 Write a program in Python to create a text file "myfile.txt" and write List items EOL lEndof lin)
containing your name, address and mobile nurmber.
9. Define a function WRITE() to create a text file "YOU.txt" and write five lines ) wu Us a alulimikThu uis no detimik
about COVID-19. Define a function READ() to read from "YOU.xt" and display
one line at a time. Call both the functions. n w im ln
10. Definea function WRITE() to create a text file "country.Lxt" and write five lines
about our country. Define a function READ() to read from "country.txt" using
readlines() and show the contents on screen. Call both the functions.
3) ut can bt steuud Lean hoT Shod
humaun radabl umaur tadablu
oum am.
u) EctenaLe .tet EclmsuOL bn
Aw Binay ulw u a s t u LthCu t e t TLadlinL )
bicauae hinauy ls o Can Lel wut- troct ):
rLad y Cempuliu nly aund
Fapen
l'You. tt', 'wo')
e n nst stoud win hinnuread- F. wut ' Co vLo 9 stauds CeuonaLY
-ablu om. oliaLase oL9 \n')
F. witt omamon cludu
.tx t
Sumpttmy
gun a ct A , C6Lgh ahouuso
ils bratn ln')
F w i t ThuL uuw u
s .bin Linen to a pmauulty
binauy ilu prLadbelunn peopis durug
Lentaet
F witu Tim tom epsawn
uitin4 to a luct ila4 typicuuy asuud 2
f- &pu puuen tet", w) oouys IuY
F. wiut
F. wut
hanudwemk w aky to
Succsas.l F.wit t t w a y fit ideuifi d tn
on4 s n but pouty.l
F. clae )
Ldteemhuu io19 n )
PTuntt datawitteu u thu Hils du rad- tet):
FGpun (' you.tet ')
Ss *Wetiug list tums t a tuct HlL lu E. radLinu
z

h i l Uus
F p r l myHiu et',w') prui lLin
L hamL -chslntaln', add palual u E . ralinu )
phu ho.9034562.32
F. wuite Linw lL)
witttet
F.CLsl)
PTiut ('list tem wmitte Succissuly)

GOoD t7.217E
Mslo UsL reaollinuD
ou wite Ht : -
GOLAYA PROGRESSIVE PUBLIC SCHOOL
tz pen l'tsunbuy. tet w )
F.wit'India "isa beauHul Count
F. i t l't is ich un Cuutu auol Assignment -2
Hadih' em lh) Define a function COUNT) to read from
F. w i t TL Coutny is Sussnded country.txt" and count the number of lines.
by tu 3 seuäns\n') Define function COUNTTO to
a
Fwite mdia as i t t plans read from
country.txt" and count the number of lines
starting with "T".
t e ln')
u t he Define a function COUNTNTO to read from
nouthounpltus paut
Sudiq is CSUnd uiH "country.txt" and count the number of lines
mLntaius \u') not starting with T.
F.elssO
d e read tesetl): Css
F pem l'Ceuty.tet' ta opelcsuuy txt','y')
i F:readlidl)
f. radlinel)
while Liue
PTint luinn)
Cnt t:1
Feles pYnt 'tetal lins au' Colwt)
wuit ttoet ) F. closL0
Tad terot
F opn ony tk t )
Cotut

E-opn louty . txtY)


F. radtins)
Cnt:ED

GooD R I TE
T'):
i (u[o
: =

Cont t =||
lins stanting fum T , Cotunt)
PTnt " total
. cl
Centuy.txt" r')
Fa Op ltitie
F Ladlinewl)

i i [ a = (7):
ontt
net slonling
prunt l Tolal lius a u L
L m T " , Cont)
GOLAYA PROGRESSIVE PUBLIC SCHOOL

Assignment -4
Write a program to count how many times "to" appears as an
independent word in a text file.
Write a program to count number of capital alphabets in a text
file.
Write a program that appends the contents of one text file to
another. Ask the filenames from the user.
Write a function rem_lower( ) that accepts two filenames. and
copies all lines that do not start with lowercase letter from the
first file into the second file.

Gusi fa Open l'You.txt', r )


Cont=o
S F. rad l)
xS.Speite )
u i 'to):
s n t Couttl
p rint lcont)
"f. clese ()-

Cuws Fa chunl'tcwntytet','')
Cotnt D
S E Lad ()

i i isuppu):
Lsunt Lewt tl
700 P t[lut)
Flosl)
du em- owes ):
ri p u t ("Erlu tist fil namL") lrtnn
F2 v p u t E u sLvid filu hame) Rulatise fa
a peu (f1, ' )
b pen Fa salul Pa Relatue Pat
X a radluuy l)
fo uL X:
talways ugius t s relatwve +o Hu
u (uLo].talsuer) with Hu 'rott toiduw Progums Cuet
Centiuu
else
b. ele wut i) A SLngu peuod ("dot") #C a fodx name
is shund thiw olulbey
b. clss)
d u c t - r a d ():
i nam input Ctmtn tilhami
wo bUlcds ("det det )maw he pareut
foldi
unamu, tr})
Fread Examp 5 elUur pat
elAL)
rem -

k ow)
bacsN
Ibatal f
uct Ownl)
bacen
elkappund- txt ):
Umame 1 uput cutu
Ecampl o bsalutu path
fuwnam
F pen filoname', ta') G:Tbacen l ar
F opun 'd.t ','r') bacem
FaS Fa. rad ) C: 1bacen
Fi wuitt Is)
F TlL0
Fa.clssel) appud tt0
du ttxt -readl): ttyt read )
FOpu lb.txt, 'r)|
P i n t a L y

cls&
wnnt Woukng irectty *wutu a program w Pyther toLwut
rts n cu
Euuy p rO9ram at Stoun i,euc
cgmputi has wsut wokng toa n a y Hlu "a-dat"
olpectouy ovd.
OS get Cúdl) o wite bunas U:
os. thdi) Fu pen "a. dat ", "wb")
Ent tu tourwung untt t nin
List[I,>3,4,5%
eHu h l :
wmpot picklu
imp6t Os
pickl dump ist,flu)
a s gut Gwd D
prunt " st addid to bunany Lt)
C I PHthn 34 wit binay
7 Os-Chdu L'C: uindsus systim 32)
227 Os guroud) pick. Uoad i sLd t ad u bbguct
CI windsusIl system 32
BINARY FILE OPERATION S UutL aPTe Yramm m Pytmm to rLad
a tuuotwe"
The ntion pTDvLdud wn Python tL
Ldichhoany
Lem buany lb.da!
wung readug filt wetk wiH
SUng Mamatbw oluk nOdbun ):
File
Pickle moduteuw usd to Cpul " b dat", "tb")
Rund ebguet filu as Lit
Vmpet plck
alleis D pick Load P l
to steu Pytheu tbjiet w H Pov nt D)
Stu Suehue Fi CLI)
rAad- biul)
u tt proics m a n n c H ens
Ppickle dump() s Cussel to wu
ttu bgcts
L L A - Au
UD. rr nUN the number of tuunes "n ccurs in the tile
ASSIGNMENT (DATA FILE HANDLING - TEXT FILES) For example if the file "DATA TXT"
contains
I mark questions This is my website. I have
1. Differentiate between file modes rt and rb+ with respect to Python
displayed my pretferences in the CtHOICE section
The countmy( ) function should display the output as
2. Differentiate betwcen file modes rt and wt with respect to Python.
"my occurs 2 times"
3. Differentiate between file modes rt and r with respect to Python
7. Write a method in Phyton
4. Differentiate betwecen the
to recad lines from a text file DIARY TXT, and
following those lines, which display
i)f open ('diary. txt', r) starting with an alphabet "P"
are

(1)f-open('diary. txt, 'w)


8.
Write a method/function DISPLAYWORDS) in python to read lines from a text file
5. Write STORY.TXT, and display those words, which are less
a statement in
Python to perform the following operations. than 4 characters.
9. Write a method/function ABLINES()
.To open a text file "BOOK TXT" in read mode in
python to read contents from a text file
To open tex1 file "BOOK TXT" in LINES.TXT, to display those lines, which are cither
a
writc mode starting with an
alphabet 'A' or
6. Observe the starting with alphabet 'B'.
following code and answer the questions that follow
File =open("Mydata","a") For example:
If the content of the file is
#Blank1
File.closc(O
What type A BOY IS PLAYING OUTSIDE
(Text/Binary) of file is Mydata?
THE PLAYGROUND IS BIG
i Fill the Blank I with statement to write "ABC" in the file
"Mydata"
7. Write BANYAN TREE IS IN THE GROUND
a statement in Python to open a text file DATA.TXT so that new contents can
be written in it.
8. Write a statement in Python to open text file
a README.TXT so that existing content The method/function should display
can be read from it.
A BOY IS PLAYING
2 mark questions OUTSIDE
1. Write method in BANYAN TREE IS IN THE GROUND
a
python to write multiple line of text contents into a text file
myfile.txt ine.
2. Write a method in
b
Python to read lines froma text file MYNOTES.TXT, and display
those lines, which are
wil opu ww 0peu
starting with the alphabet 'K".
3. Write method in python to recad the content
a
from a text file diary.txt line by line and
bet YLading bsth
display the same on screen. rudi ng nd
4. Write a method in Python to read lines from a text file NOTES.TXT, and count those
Cnd witn wutin pulpes&e.
lines, which ending with' or ''
are upos
5. A lext file "Quotes.Txt" has the following data written in it:
Living a life you can be proud of

Doing your best w


Spending your time with people and activities that are
important to you
t wwuLl p a st w i Open a
Standing up for things that are right even when it's hard
Becoming the best version of you beth radung aud
Rile uu tut te
mat
Write a neer defined tiinction 1n dicnlav the total mumher nf worde nrecent in the file wutun betL witini
GOOÐ WRITE GOOD wRI TE RAadng
w Ywi Dpun t
Fapn l'DATR . TXT, 'a)
t wi op tut
tuxt filu to FGpenl 'REA DME. Tal )
t bsth LOding nty+ 22F radlinul0
Ladung aud
wwitin PTintle)
2 F:uaulinel)

i p n l diauy. tyt, ' r )


ut Lttt is cpened t e i readng data Mak qush e
Cus rite )
u) F opun diany. tx, w')
us TAadliny )
ut ww Lepenud fa wulung data
(us3TLadluu l)
ouS F:_spul'gced F t r Ou Teadlius l)
F pe BooK. TXI'Y') Quss
F.6
FCpa '6oo k. TTw') TAadlius U

B1 TLad
uas6 1 Text ypt o til i Mydala

F:witeABC.\n)

GOC twR)TE

You might also like