forked from AEC-Tech/Python-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooks.py
63 lines (54 loc) · 1.35 KB
/
books.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pickle
def createFile():
bno = int(input("Enter Book number : "))
name = input("Enter book title : ")
author = input("Enter author : ")
price = int(input("Enter price : "))
bookdata = [bno,name,author,price]
f = open("books.dat","ab")
pickle.dump(bookdata, f)
f.close()
def countRec(author):
f = open("books.dat","rb")
bk = []
count = 0
try:
while True:
bk = pickle.load(f)
if bk[2] == author:
count += 1
print(bk)
except EOFError:
f.close()
print("Number of books written by ",author," is", count)
def updateBook(bid,ncost):
f = open("books.dat","rb+")
bk = []
count = 0
try:
while True:
pos = f.tell()
bk = pickle.load(f)
if bk[0] == bid:
bk[3] = ncost
f.seek(pos)
pickle.dump(bk,f)
except EOFError:
f.close()
def displayBooks():
f = open("books.dat","rb")
bk = []
print("Books available are ")
try:
while True:
bk = pickle.load(f)
print(bk)
except EOFError:
f.close()
displayBooks()
updateBook(15,120)
displayBooks()
#n= int(input("How many books are to be added : "))
#for a in range(n):
# createFile()
#countRec("Anjali")