-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileOperations.py
57 lines (42 loc) · 1.6 KB
/
FileOperations.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
import os
from typing import Union
class FileOperations:
"""
A class to write the contents of a variable to a file in binary form.
Attributes:
file_path (str): The path to the file where the binary data will be written.
"""
CLASS_VERSION = "0.01"
def __init__(self, file_path: str):
"""
Initializes the BinaryFileWriter with the specified file path.
Args:
file_path (str): The path to the file where the binary data will be written.
"""
self.file_path = file_path
def ensure_directory(self) -> None:
"""
Ensures that the directory for the file path exists. If not, it creates the necessary directories.
"""
directory = os.path.dirname(self.file_path)
if not os.path.exists(directory):
os.makedirs(directory)
def write_binary(self, content: bytes) -> None:
"""
Writes the provided content to the file in binary form.
Args:
content (bytes): The binary data to be written to the file.
Raises:
ValueError: If the content is not of type bytes.
IOError: If there is an issue writing to the file.
"""
if not isinstance(content, bytes):
raise ValueError("Content must be of type bytes.")
# Ensure the directory exists
self.ensure_directory()
try:
with open(self.file_path, 'wb') as file:
file.write(content)
except IOError as e:
print(f"An error occurred while writing to the file: {e}")
raise