Sunday, July 12, 2020

File mode Difference 'r' 'w' 'r+' 'w+', Difference in File Pointer Position and Accessing File

modedifferencer,w,r+,w+
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())
hello,wordd
how are you
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()
---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-6-f7f0376bdbc2> in <module>
      1 f=open('out.txt','r')
----> 2 f.write('can be write')

UnsupportedOperation: not writable
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()
yes both read and write possible te possible yes both read and write possible yes both read and write possible yes both read and write possible yes both read and write possible yes both read and write possible yes both read and write possible yes both read and write possible 
321

321
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()
filepointer location 0
practice if u can 
problem is opportunity
can you understand can you understand can you understand 
filepointer location 101
filepointer location 120
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()
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-30-058017981d1e> in <module>
----> 1 f=open('out2.txt','r+') #r+ mode file will not create if open in r+,but File will create if open in 'w' mode
      2 f.write('now check this mode')
      3 print(f.read())
      4 f.close()

FileNotFoundError: [Errno 2] No such file or directory: 'out2.txt'
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())
filepointer location 0
filepointer location 15

filepointer location 0
now next mode
the end of file
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)
readinig file pass 0 practice if u can 
problem is opportunity
can you understand can you understand can you understand can you understand 
readinig file pass 1 practice if u can 
problem is opportunity
can you understand can you understand can you understand can you understand 
readinig file pass 2 practice if u can 
problem is opportunity
can you understand can you understand can you understand can you understand 
readinig file pass 3 practice if u can 
problem is opportunity
can you understand can you understand can you understand can you understand 
readinig file pass 4 practice if u can 
problem is opportunity
can you understand can you understand can you understand can you understand 
In [ ]:
 

No comments:

Post a Comment