Sunday, July 5, 2020

Python binary file program and Practice for ,For class 12


binaryfilepractice
In [2]:
#binary file creation using open function
def bfile(binf):
    open(binf,'wb')
    
    
In [3]:
bfile("bin1.dat")
In [4]:
bfile('bin2.dat')
In [5]:
def bfile(binf):
    import pickle
    li=[10,20,30]
    f=open(binf,'wb')
    pickle.dump(li,f)
    f.close()
    #writing a list in binary file
In [7]:
bfile('bin1.dat') #calling function to write in file bin1.dat
In [8]:
bfile('bin2.dat')
In [11]:
def bprint(bnam):
    import pickle
    f=open(bnam,'rb')
    l=pickle.load(f)
    print('file:----',f.name,' contained',l)
    
    
In [12]:
bprint('bin1.dat')
file:---- bin1.dat  contained [10, 20, 30]
In [13]:
bprint('bin2.dat')
file:---- bin2.dat  contained [10, 20, 30]
In [14]:
def bfile2(binf,l):
    import pickle
    f=open(binf,'wb')
    pickle.dump(l,f)
    f.close()
In [15]:
bfile2('bin1.dat',[1,2,3])
In [16]:
bfile2('bin2.dat',[3,4,5])
In [17]:
bprint('bin1.dat')
file:---- bin1.dat  contained [1, 2, 3]
In [18]:
bprint('bin2.dat')
file:---- bin2.dat  contained [3, 4, 5]
In [20]:
def bfile3app(binf,l):
    import pickle
    f=open(binf,'ab')
    pickle.dump(l,f)
    f.close()
In [21]:
bfile3app('bin.dat',[7,8,9])
In [22]:
bfile3app('bin1.dat',[11,12,13])
bfile3app('bin2.dat',[22,33,44,55])
In [23]:
bprint('bin1.dat')
file:---- bin1.dat  contained [1, 2, 3]
In [24]:
bprint('bin2.dat')
file:---- bin2.dat  contained [3, 4, 5]
In [3]:
def bprint(bnam):
    import pickle
    f=open(bnam,'rb')
    l=pickle.load(f)
    print('file:----',f.name,' contained',l)
    l=pickle.load(f)
    print('file:----',f.name,' contained',l)
    l=pickle.load(f)
    print(l)
    
In [4]:
bprint('bin1.dat')
file:---- bin1.dat  contained [1, 2, 3]
file:---- bin1.dat  contained [11, 12, 13]
---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-4-7918cec1c4cc> in <module>
----> 1 bprint('bin1.dat')

<ipython-input-3-c5f2db2492f3> in bprint(bnam)
      6     l=pickle.load(f)
      7     print('file:----',f.name,' contained',l)
----> 8     l=pickle.load(f)
      9     print(l)
     10 

EOFError: Ran out of input
In [5]:
bprint('bin2.dat')
file:---- bin2.dat  contained [3, 4, 5]
file:---- bin2.dat  contained [22, 33, 44, 55]
---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-5-a954efc947cb> in <module>
----> 1 bprint('bin2.dat')

<ipython-input-3-c5f2db2492f3> in bprint(bnam)
      6     l=pickle.load(f)
      7     print('file:----',f.name,' contained',l)
----> 8     l=pickle.load(f)
      9     print(l)
     10 

EOFError: Ran out of input
In [4]:
def binwrite(data):
    import pickle 
    f=open('bindata.dat','ab')
    pickle.dump(data,f)
    f.close()
  
In [5]:
binwrite([1,2,3])
In [ ]:
binwrite((4,5,6))
In [ ]:
n=input('enter anything for file input')
binwrite(n)
In [16]:
f=open('bindata.dat','rb')
import pickle
for i in range(7):  
    w=pickle.load(f)
    print(type(w))
    print(w)
    print('file pointer ',f.tell())
f.seek(0,0)
print('file pointer ',f.tell())
f.seek(0,1)
print('file pointer ',f.tell())
f.seek(0,2)
print('file pointer ',f.tell())
f.seek(0,1)
print('file pointer ',f.tell())
<class 'list'>
[1, 2, 3]
file pointer  14
<class 'tuple'>
(4, 5, 6)
file pointer  26
<class 'dict'>
{'a': 1, 'b': 2, 'c': 3}
file pointer  64
<class 'dict'>
{'a': 1, 'b': 2, 'c': 3}
file pointer  102
<class 'str'>
india
file pointer  117
<class 'str'>
'india'
file pointer  134
<class 'str'>
1213
file pointer  148
file pointer  0
file pointer  0
file pointer  148
file pointer  148
In [12]:
# Open a file
fo = open("foo.txt", "a+")
print ("Name of the file: ", fo.name)

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
fo.write("happy sunday \n")
fo.write("happy gurupurnima \n")
line = fo.readline()
print ("Read Line: %s" % (line))

# Again set the pointer to the beginning
fo.seek(0, 0)
line = fo.readline()
print("Read Line: %s" % (line))
line = fo.readline()
print("Read Line: %s" % (line))

# Close opend file
fo.close()
Name of the file:  foo.txt
Read Line: 
Read Line: happy sunday 

Read Line: happy gurupurnima 

In [35]:
def readbintext(f):#reading binary file in text mode
    fl=open(f,'r')
    print(fl.name)
    d=fl.read()
    print(d)
    
 
In [36]:
readbintext('bindata.dat')  #getting garbage type data and its not understable because its binary fomrat 
bindata.dat
€]q (KKKe.€KKK‡q .€}q (X   aqKX   bqKX   cqKu.€}q (X   aqKX   bqKX   cqKu.€X   indiaq .€X   'india'q .€X   1213q .
In [43]:
def readbintext(f):#reading binary file in text mode 
    fl=open(f,'r')
    print("file name is",fl.name)
    # print('file position',fl.tell())
    fl.seek(0,2)
    pos=fl.tell()
    print('file eof postition',fl.pos)
    fl.seek(0,0)
    p=0
    while(p<=pos):
        print(fl.readline())
        p=fl.tell()
        
In [45]:
readbintext('bindata.dat')
file name is bindata.dat
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-45-67bec902d9c3> in <module>
----> 1 readbintext('bindata.dat')

<ipython-input-43-989f0acce25a> in readbintext(f)
      5     fl.seek(0,2)
      6     pos=fl.tell()
----> 7     print('file eof postition',fl.pos)
      8     fl.seek(0,0)
      9     p=0

AttributeError: '_io.TextIOWrapper' object has no attribute 'pos'
In [46]:
def readtextbin(fil):
    f=open(fil,'rb')
    import pickle
    data=pickle.load(f)
    print(data)
    f.close()
In [47]:
readtextbin('covid.txt')
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-47-c26caf284c84> in <module>
----> 1 readtextbin('covid.txt')

<ipython-input-46-2b97f899b242> in readtextbin(fil)
      2     f=open(fil,'rb')
      3     import pickle
----> 4     data=pickle.load(f)
      5     print(data)
      6     f.close()

ModuleNotFoundError: No module named 'orona virus '
In [48]:
def readtextbin(fil):
    f=open(fil,'r')
    d=f.read()
    print(d)
In [49]:
readtextbin('covid.txt')
corona virus 
keep safe yourself
protect yourself 
keep yourself quarantine
In [8]:
def readimg(fl):
    f=open(fl,'rb')
    import pickle
    print(pickle.load(f))
    
    
In [3]:
import os
print(os.getcwd())
C:\Users\Acer
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

No comments:

Post a Comment