From cb48fd3e627d028966d772bd6d146e2cddc6d9d6 Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Mon, 17 Jul 2023 21:05:33 +0200 Subject: [PATCH] file --- file/__init__.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 file/__init__.py diff --git a/file/__init__.py b/file/__init__.py new file mode 100644 index 0000000..f622b10 --- /dev/null +++ b/file/__init__.py @@ -0,0 +1,35 @@ +import os +import shutil +import subprocess + +CHARSET = 'UTF-8' + + +def create_image(file_path: str, size_bytes: int): + subprocess.call([ + 'qemu-img', + 'create', + '-f', 'qcow2', + file_path, + size_bytes, + ]) + + +def empty(file_path: str): + write(file_path, str()) + + +def make(directory_path: str): + os.makedirs(directory_path, exist_ok=True) + + +def wipe(path: str): + try: + shutil.rmtree(path) + except NotADirectoryError: + os.remove(path) + + +def write(file_path: str, text: str, charset=CHARSET): + with open(file_path, 'bw') as file_object: + file_object.write(text.encode(charset))