Friday, July 3, 2020

File Handling Practice Pyhton class 12


filehandling
In [13]:
f=open('debugging.ipynb')
print('file name',f.name)
print('filemode',f.mode)
print('is file readable',f.readable())
print('is file closed',f.closed)
f.close()
print('file name',f.name)
print('filemode',f.mode)
print('is file readable',f.readable())
print('is file closed',f.closed)
file name debugging.ipynb
filemode r
is file readable True
is file closed False
file name debugging.ipynb
filemode r
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-9239040e8c37> in <module>()
      7 print('file name',f.name)
      8 print('filemode',f.mode)
----> 9 print('is file readable',f.readable())
     10 print('is file closed',f.closed)

ValueError: I/O operation on closed file
In [14]:
import os
f=open('jnv.txt')
data=f.read()
print(data)
print(os.getcwd())
f.close()
navoadayavidyalaya
C:\Users\KKK
In [9]:
import os
c=os.getcwd()
print(c)
C:\Users\KKK
In [16]:
fil=open('jnv.txt','w')
In [19]:
fil.write('navoadaya')
fil.write('vidyalaya kundeshwar tikamgarh')
fil.write('computer science with python ')
Out[19]:
29
In [22]:
fil.close()
fil=open('jnv.txt','r')
t=fil.readlines()
print(t)
fil.close()
['navoadayavidyalayakundeshwartikamgarhnavoadayavidyalaya kundeshwar tikamgarhnavoadayavidyalaya kundeshwar tikamgarhcomputer science with python ']
In [4]:
fil.close()
fil=open('jnv.txt','r')
t=fil.readline()
print(t)
navoadayavidyalaya
In [9]:
fil.close()
fil=open('jnv.txt','r')
t=fil.read()
print(t)
navoadayavidyalaya
In [11]:
fil.close()
fil=open('jnv.txt','r')
t=fil.read(10)
print(t)
navoadayav
In [11]:
fil.close()
fil=open('jnv.txt','r')
for line in fil:
     print(line)
navoadayavidyalaya
In [13]:
fil.close()
fil=open('jnv.txt','r')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-f1a5bb70ae57> in <module>()
      1 fil.close()
      2 fil=open('jnv.txt','r')
----> 3 print(len(fil))

TypeError: object of type '_io.TextIOWrapper' has no len()
In [16]:
fil.close()
fil=open('jnv.txt','r')
print(fil.read(1))
print(fil.read(2))
print(fil.read())
fil.close()
n
av
oadayavidyalaya
In [20]:
with open('jnv.txt','r') as f:
     print(f.read(1))
     print(f.read(2))
     print(f.read())
n
av
oadayavidyalaya
In [3]:
f=open('jnvnew.txt','w')
f.write('navodya vidyalaya')
f.write('kundeshwar')
f.close()
f=open('jnvnew.txt','r')
j=f.read()
print(j)
navodya vidyalayakundeshwar
In [7]:
f=open('jnvnew.txt','a')
f.write('tikamgarh')
f.write('madhyapradesh')
f.close()
f=open('jnvnew.txt','r')
print(f.read())
f.close()
navodya vidyalayakundeshwartikamgarhmadhyapradeshtikamgarhmadhyapradeshtikamgarhmadhyapradeshtikamgarhmadhyapradesh
In [13]:
r=open('jnvnew.txt','r')
data=r.read();
data.split()
for j in data:
    print(j)
data.close()
n
a
v
o
d
y
a
 
v
i
d
y
a
l
a
y
a
k
u
n
d
e
s
h
w
a
r
t
i
k
a
m
g
a
r
h
m
a
d
h
y
a
p
r
a
d
e
s
h
t
i
k
a
m
g
a
r
h
m
a
d
h
y
a
p
r
a
d
e
s
h
t
i
k
a
m
g
a
r
h
m
a
d
h
y
a
p
r
a
d
e
s
h
t
i
k
a
m
g
a
r
h
m
a
d
h
y
a
p
r
a
d
e
s
h
In [15]:
n=open('name.txt','a')
while True:
     nam=input('enter name for entry enter quit for exit')
     n.write(nam)
     if nam=='quit':
            break
n.close()
    
     
enter name for entry enter quit for exitdevvrat
enter name for entry enter quit for exitshivi
enter name for entry enter quit for exitvinita
enter name for entry enter quit for exitravi
enter name for entry enter quit for exitbharat
enter name for entry enter quit for exitjyoti
enter name for entry enter quit for exiterika
enter name for entry enter quit for exitdau
enter name for entry enter quit for exitmanju
enter name for entry enter quit for exitsuman
enter name for entry enter quit for exithardayal
enter name for entry enter quit for exitmanohar
enter name for entry enter quit for exitroopnarayan
enter name for entry enter quit for exitjainarayan
enter name for entry enter quit for exitsatyanarayan
enter name for entry enter quit for exitjitendra
enter name for entry enter quit for exitdharmanedra
enter name for entry enter quit for exitarvindra
enter name for entry enter quit for exitsunita
enter name for entry enter quit for exitsantosh
enter name for entry enter quit for exitquit
In [17]:
fname=input('enter filename')
f=open(fname,'r')
data=f.read()
print(data)
enter filenamename.txt
devvratshivivinitaravibharatjyotierikadaumanjusumanhardayalmanoharroopnarayanjainarayansatyanarayanjitendradharmanedraarvindrasunitasantoshquit
In [19]:
fname=input('enter filename')
f=open(fname,'w')
enter filenameshivi.txt
In [2]:
fname=input('enter filename')
f=open(fname,'w')
while True:
    n=input('enter number to feed and quit for exit')
    if(n=='0'):
         break
    f.write(n)
f.close()
f=open(fname,'r')
print(f.read())
enter filenamename1
enter number to feed and quit for exithello
enter number to feed and quit for exithi
enter number to feed and quit for exithow 
enter number to feed and quit for exitare
enter number to feed and quit for exityou
enter number to feed and quit for exit0
hellohihow areyou
In [ ]:
 
In [ ]:
 

No comments:

Post a Comment