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')
In [13]:
bprint('bin2.dat')
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')
In [18]:
bprint('bin2.dat')
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')
In [24]:
bprint('bin2.dat')
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')
In [5]:
bprint('bin2.dat')
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())
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()
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
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')
In [46]:
def readtextbin(fil):
f=open(fil,'rb')
import pickle
data=pickle.load(f)
print(data)
f.close()
In [47]:
readtextbin('covid.txt')
In [48]:
def readtextbin(fil):
f=open(fil,'r')
d=f.read()
print(d)
In [49]:
readtextbin('covid.txt')
In [8]:
def readimg(fl):
f=open(fl,'rb')
import pickle
print(pickle.load(f))
In [3]:
import os
print(os.getcwd())
In [ ]:
In [ ]:
In [ ]:
In [ ]:
No comments:
Post a Comment