-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPracticeBinary.py
More file actions
36 lines (31 loc) · 952 Bytes
/
Copy pathPracticeBinary.py
File metadata and controls
36 lines (31 loc) · 952 Bytes
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
import os
import struct
RECORDSTRUCTURE = '10si'
class record():
def __init__(self):
self.name = ""
self.age = 0
person = record()
recordSize = struct.calcsize(RECORDSTRUCTURE)
file = open('File.dat', 'ab+')
file.seek(0, os.SEEK_END)
fileSize = file.tell()
records = int(fileSize / recordSize)
for i in range(4):
person.name = input("Enter a name: ")
person.age = int(input("Enter a age: "))
data = struct.pack(RECORDSTRUCTURE, bytes(person.name, ('ascii')), person.age)
file.write(data)
file.close()
file = open('File.dat', 'rb')
file.seek(0, os.SEEK_END)
fileSize = file.tell()
records = int(fileSize / recordSize)
for i in range(records):
file.seek(i * recordSize)
data = file.read(recordSize)
person.name , person.age = struct.unpack(RECORDSTRUCTURE, data)
print("Name is: ", person.name.decode())
print("Age is: ", person.age)
print()
file.close()