Binary File Exercise
#Write a program to read and write programming
languages
#stored in the tuple to binary file [Link].
import pickle
def bin2tup():
f = open("[Link]","wb")
t = ('C','C++','Java','Python')
[Link](t,f)
[Link]()
f = open("[Link]","rb")
d = [Link](f)
print("PL1:",d[0])
print("PL2:",d[1])
print("PL3:",d[2])
print("PL4:",d[3])
[Link]()
bin2tup()
'''Write a program to store customer data into a binary
file [Link] using a dictionary and print them on screen
after reading them. The customer data contains ID as
key, and name, city as values.'''
import pickle
def bin2dict():
f = open("[Link]","wb")
d = {'C0001':['Subham','Ahmedabad'],
'C0002':['Bhavin','Anand'],
'C0003':['Chintu','Baroda']}
[Link](d,f)
[Link]()
f = open("[Link]","rb")
d = [Link](f)
print(d)
[Link]()
bin2dict()
''' Write a function to write data into binary file
[Link] and display the records of students who
scored more than 95 marks.'''
import pickle
def search_95plus():
f = open("[Link]","ab")
while True:
rn=int(input("Enter the rollno:"))
sname=input("Enter the name:")
marks=int(input("Enter the marks:"))
rec=[]
data=[rn,sname,marks]
[Link](data)
[Link](rec,f)
ch=input("Want more records?Yes:")
if [Link]() not in 'yes':
break
[Link]()
f = open("[Link]","rb")
cnt=0
try:
while True:
data = [Link](f)
for s in data:
if s[2]>95:
cnt+=1
print("Record:",cnt)
print("RollNO:",s[0])
print("Name:",s[1])
print("Marks:",s[2])
except Exception:
[Link]()
search_95plus()
Count All Record from file
import pickle
def count_records():
f = open("[Link]","rb")
cnt=0
try:
while True:
data = [Link](f)
print(data)
cnt+=1
except Exception:
[Link]()
print("The file has ", cnt, " records.")
count_records()
''' Write a program to know the cursor position and
print the text according to below-given specifications:
Print the initial position Move the cursor to 4th position
Display next 5 characters Move the cursor to the next
10 charactersPrint the current cursor position Print next
10 characters from the current cursor position'''
def program9():
f = open("[Link]","r")
print([Link]())
print([Link](4))
[Link](4,0)
print([Link](5))
[Link](10,0)
print([Link]())
print([Link](7,0))
print([Link](10))
program9()