-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.24.py
More file actions
80 lines (58 loc) · 1.75 KB
/
15.24.py
File metadata and controls
80 lines (58 loc) · 1.75 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class IncorrectData(Exception):
def __init__(self, n):
self.n = n
def __str__(self):
return f'{self.n} is incorrect'
class FileProblem(Exception):
def __init__(self, fname, msg):
self.fname = fname
self.msg = msg
def __str__(self):
return f'{self.fname} is not open: {self.msg}'
class WorkBinFile:
def __init__(self, fname):
self.fname = fname
self.num = 0
def input_console(self):
n = int(input('n = '))
if n <= 0:
raise IncorrectData(n)
f = open(self.fname, 'wb')
if not f:
raise FileProblem(self.fname, 'cannot open')
for _ in range(n):
x = int(input('x = '))
f.write(x)
f.close()
def write_from_list(self, lst):
f = open(self.fname, 'wb')
if not f:
raise FileProblem(self.fname, 'cannot open')
for x in lst:
f.write(bytearray([x]))
f.close()
def read(self):
f = open(self.fname, 'rb')
if not f:
raise FileProblem(f, 'cannot read')
while True:
x = f.read(1)
x = int.from_bytes(x, byteorder='big')
print(x)
if not x:
break
f.close()
def append(self, data):
f = open(self.fname, 'a+b')
if not f:
raise FileProblem(self.fname, 'cannot append')
f.write(bytearray(data))
f.close()
if __name__ == "__main__":
try:
wf = WorkBinFile('1.dat')
wf.write_from_list([1, 2, 3, 4, 7, 4, 8, 9])
wf.append(8)
wf.read()
except FileProblem as e:
print(e)