create and delete files

This commit is contained in:
WorldTeacher
2024-02-07 11:37:31 +01:00
parent 244bd0bb25
commit 11fdae1553
2 changed files with 46 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
import os
from pathlib import Path
from omegaconf import OmegaConf
from src.backend.database import Database
db = Database()
config = OmegaConf.load("config.yaml")
def recreateFile(name, app_id, filetype, open=True)->Path:
"""
recreateFile creates a file from the database and opens it in the respective program, if the open parameter is set to True.
Args:
----
- name (str): The filename selected by the user.
- app_id (str): the id of the apparatus.
- filetype (str): the extension of the file to be created.
- open (bool, optional): Determines if the file should be opened. Defaults to True.
Returns:
-------
- Path: Absolute path to the file.
"""
path = db.recreateFile(name, app_id, filetype=filetype)
path = Path(path)
if open:
if os.getenv("OS") == "Windows_NT":
path = path.resolve()
os.startfile(path)
else:
path = path.resolve()
os.system(f"open {path}")
return path

View File

@@ -0,0 +1,13 @@
import os
from omegaconf import OmegaConf
config = OmegaConf.load("config.yaml")
def delete_temp_contents():
"""
delete_temp_contents deletes the contents of the temp directory.
"""
for root, dirs, files in os.walk(config.database.tempdir):
for file in files:
os.remove(os.path.join(root, file))
for dir in dirs:
os.rmdir(os.path.join(root, dir))