-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.py
More file actions
52 lines (39 loc) · 1.27 KB
/
Copy pathmessage.py
File metadata and controls
52 lines (39 loc) · 1.27 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
import numpy
import sys
import os
def read_file(location):
"""Open the file and return an array containing the bytes
Parameters:
location: Location of the file
Returns:
Array of bytes in integer type
"""
array_bytes = None
try:
array_bytes = numpy.fromfile(location, dtype="uint8")
except FileNotFoundError:
print("The file doesn't exist or no read permissions")
sys.exit()
return array_bytes
def write_file(location, byte_array):
"""Create a file with the given byte array (integer values)
Parameters:
location: Location and name of the file
byte_array: The data
Returns:
True if it succeeds otherwise False
"""
try:
# check if a directory was provided
if os.path.dirname(location) is not '':
# create directory if don't exist
os.makedirs(os.path.dirname(location), exist_ok=True)
# convert the data to binary array
byte_array = bytearray(byte_array)
# write to the file
with open(location, 'wb') as f:
f.write(byte_array)
except PermissionError:
print("The message file doesn't exist or no read permissions")
sys.exit()
return True