-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryFile.py
More file actions
43 lines (36 loc) · 1.23 KB
/
Copy pathBinaryFile.py
File metadata and controls
43 lines (36 loc) · 1.23 KB
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
import os
import struct
class StuRecord():
def __init__(self):
self.Id = 0
self.Name = ""
self.Class = ""
self.Fee = 0.0
Student = StuRecord()
RECORDSTRUCTURE = 'i20s4sf'
RecordSize = struct.calcsize(RECORDSTRUCTURE)
file = open("BinaryFile.dat", 'ab+')
file.seek(0, os.SEEK_END)
FileSize = file.tell()
NoOfRecords = int(FileSize / RecordSize)
Student.Id = NoOfRecords + 1
Student.Name = input("Enter your name here: ")
Student.Class = input("Enter your class here: ")
Student.Fee = float(input("Enter your fee here: "))
data = struct.pack(RECORDSTRUCTURE, Student.Id, bytes(Student.Name, 'ascii'), bytes(Student.Class, 'ascii'), Student.Fee)
file.write(data)
file.close()
file = open("BinaryFile.dat", 'rb')
file.seek(0, os.SEEK_END)
FileSize = file.tell()
NoOfRecords = int(FileSize / RecordSize)
for i in range(NoOfRecords):
file.seek(i* RecordSize)
data = file.read(RecordSize)
Student.Id, Student.Name, Student.Class, Student.Fee = struct.unpack(RECORDSTRUCTURE, data)
print("ID is: ", Student.Id)
print("Name is: ", Student.Name.decode())
print("Class is: ", Student.Class.decode())
print("Fee is: ", Student.Fee)
print()
file.close()