adc_toolkit.utils.manage_filesystem

Module for managing the filesystem.

 1"""Module for managing the filesystem."""
 2
 3from pathlib import Path
 4
 5
 6def create_directory(path: Path) -> None:
 7    """
 8    Create directory.
 9
10    Parameters
11    ----------
12    path : Path
13        Path to directory.
14    """
15    path.mkdir(parents=True, exist_ok=True)
16
17
18def create_file(path: Path) -> None:
19    """
20    Create file.
21
22    Parameters
23    ----------
24    path : Path
25        Path to file.
26    """
27    path.touch()
28
29
30def create_file_in_directory_if_not_exists(path: Path) -> None:
31    """
32    Create file in directory if not exists.
33
34    Parameters
35    ----------
36    path : Path
37        Path to file.
38    """
39    if not check_if_file_exists(path):
40        create_directory(path.parent)
41        create_file(path)
42
43
44def write_string_to_file(string: str, path: Path) -> None:
45    """
46    Write string to file.
47
48    This function fills the file with the string.
49
50    Parameters
51    ----------
52    string : str
53        String to write.
54    path : Path
55        Path to file.
56    """
57    with open(path, "w") as f:
58        f.write(string)
59
60
61def check_if_file_exists(path: Path) -> bool:
62    """
63    Check if file exists.
64
65    Parameters
66    ----------
67    path : Path
68        Path to file.
69
70    Returns
71    -------
72    bool
73        True if file exists, False otherwise.
74    """
75    return path.exists()
76
77
78def extract_relative_path(path: Path) -> Path:
79    """
80    Extract relative path.
81
82    This function extracts the relative path from the current working directory.
83
84    Parameters
85    ----------
86    path : Path
87        Path to file.
88
89    Returns
90    -------
91    Path
92        Relative path.
93    """
94    return path.relative_to(Path.cwd())
def create_directory(path: pathlib.Path) -> None:
 7def create_directory(path: Path) -> None:
 8    """
 9    Create directory.
10
11    Parameters
12    ----------
13    path : Path
14        Path to directory.
15    """
16    path.mkdir(parents=True, exist_ok=True)

Create directory.

Parameters
  • path (Path): Path to directory.
def create_file(path: pathlib.Path) -> None:
19def create_file(path: Path) -> None:
20    """
21    Create file.
22
23    Parameters
24    ----------
25    path : Path
26        Path to file.
27    """
28    path.touch()

Create file.

Parameters
  • path (Path): Path to file.
def create_file_in_directory_if_not_exists(path: pathlib.Path) -> None:
31def create_file_in_directory_if_not_exists(path: Path) -> None:
32    """
33    Create file in directory if not exists.
34
35    Parameters
36    ----------
37    path : Path
38        Path to file.
39    """
40    if not check_if_file_exists(path):
41        create_directory(path.parent)
42        create_file(path)

Create file in directory if not exists.

Parameters
  • path (Path): Path to file.
def write_string_to_file(string: str, path: pathlib.Path) -> None:
45def write_string_to_file(string: str, path: Path) -> None:
46    """
47    Write string to file.
48
49    This function fills the file with the string.
50
51    Parameters
52    ----------
53    string : str
54        String to write.
55    path : Path
56        Path to file.
57    """
58    with open(path, "w") as f:
59        f.write(string)

Write string to file.

This function fills the file with the string.

Parameters
  • string (str): String to write.
  • path (Path): Path to file.
def check_if_file_exists(path: pathlib.Path) -> bool:
62def check_if_file_exists(path: Path) -> bool:
63    """
64    Check if file exists.
65
66    Parameters
67    ----------
68    path : Path
69        Path to file.
70
71    Returns
72    -------
73    bool
74        True if file exists, False otherwise.
75    """
76    return path.exists()

Check if file exists.

Parameters
  • path (Path): Path to file.
Returns
  • bool: True if file exists, False otherwise.
def extract_relative_path(path: pathlib.Path) -> pathlib.Path:
79def extract_relative_path(path: Path) -> Path:
80    """
81    Extract relative path.
82
83    This function extracts the relative path from the current working directory.
84
85    Parameters
86    ----------
87    path : Path
88        Path to file.
89
90    Returns
91    -------
92    Path
93        Relative path.
94    """
95    return path.relative_to(Path.cwd())

Extract relative path.

This function extracts the relative path from the current working directory.

Parameters
  • path (Path): Path to file.
Returns
  • Path: Relative path.