-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileIO.py
64 lines (50 loc) · 1.79 KB
/
fileIO.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
64
# fileio.py
''' This file contains file I/O operations for writing and reading bytes from files.
This is useful for the storage layer. '''
from struct import pack, unpack
''' Write value to file after converting it to ascii character (1 byte). '''
def write_byte(file, val):
file.write(chr(val))
''' Write value to file by packing it into short (2 bytes). '''
def write_short(file, val):
file.write(pack('h', val))
''' Write value to file by packing it into unsigned short (2 bytes). '''
def write_unsigned_short(file, val):
file.write(pack('H', val))
''' Write value to file by packing it into int (4 bytes). '''
def write_int(file, val):
file.write(pack('i', val))
''' Write value to file by packing it into unsigned int (4 bytes). '''
def write_unsigned_int(file, val):
file.write(pack('I', val))
''' Gets the value of the ascii character (1 byte). '''
def read_byte(byte_s):
return ord(byte_s)
''' Read value of short (2 bytes). '''
def read_short(short):
return unpack('h', short)[0]
''' Read value of unsigned short (2 bytes). '''
def read_unsigned_short(short):
return unpack('H', short)[0]
''' Read value of int (4 bytes). '''
def read_int(short):
return unpack('i', short)[0]
''' Read value of unsigned int (4 bytes). '''
def read_unsigned_int(short):
return unpack('I', short)[0]
if __name__ == "__main__":
file = open('test.bin', 'wb')
write_byte(file, 10)
write_int(file, 32768)
write_unsigned_int(file, 1000000)
file.close()
file = open('test.bin', 'rb')
while 1:
byte_s = file.read(1)
int = file.read(4)
unsigned_int = file.read(4)
if not byte_s:
break
print(read_byte(byte_s))
print(read_int(int))
print(read_unsigned_int(unsigned_int))