In [3]:
#what will be output of following code
ou=open('out.txt','w')
ou.write('hello,wordd\n')
ou.write('how are you')
ou.close()
print(open('out.txt','r').read())
In [6]:
f=open('out.txt','r')
f.write('can be write') # can not write in read mode error will come 'not writable'
f.close()
In [23]:
f=open('out.txt','r+')#r+ mode means both read and write possible ,file pointer will be on 0 byte
f.write('can be write') #now its both writable and readable ,but old content is not truncating
print(f.read())
f.write('yes both read and write possible ')
print(f.tell())
print(f.read())
print(f.tell())
f.close()
In [27]:
f=open('modep.txt','w')
f.write('practice if u can \n')
f.write('problem is opportunity\n')
f.close()
In [32]:
f=open('modep.txt','r+')#its clear that in r+ mode reading ,writing possible ,but file content not truncate
print('filepointer location',f.tell()) #and file poiinter move forward in read and write operati
print(f.read())
print('filepointer location',f.tell())
f.write('can you understand ')
print('filepointer location',f.tell())
f.close()
In [30]:
f=open('out2.txt','r+') #r+ mode file will not create if open in r+,but File will create if open in 'w' mode
f.write('now check this mode')
print(f.read())
f.close()
In [42]:
f=open('modep2.txt','w+') #w+ mode both read and write possible but w+ mode truncate file and create new file if file not exit
print('filepointer location',f.tell())
f.write('now next mode\n')
print('filepointer location',f.tell())
print(f.read())
f.write('the end of file')
f.seek(0,0)
print('filepointer location',f.tell())
print(f.read())
In [45]:
#program to read and print modep2.txt file 5 times
fp=open('modep.txt','r')
for i in range(5):
print('readinig file pass',i,fp.read())
fp.seek(0,0)
In [ ]: