cement.utils.fs
Common File System Utilities.
- class cement.utils.fs.Tmp(**kwargs: str)[source]
Bases:
object
Provides creation and cleanup of a separate temporary directory, and file.
- Keyword Arguments:
cleanup (bool) – Whether or not to delete the temporary directory and file on exit (when used with the
with
operator).suffix (str) – The suffix that the directory and file will end with. Default: no suffix
prefix (str) – The prefix that the directory and file will start with. Default: no prefix
dir (str) – The parent directory path that the temp directory and file will be created in. Default: system default temporary path
Example
from cement.utils import fs with fs.Tmp() as tmp: # do something with a temporary directory os.path.listdir(tmp.dir) # do something with a temporary file with open(tmp.file, 'w') as f: f.write('some data')
- cement.utils.fs.abspath(path: str, strip_trailing_slash: bool = True) str [source]
Return an absolute path, while also expanding the
~
user directory shortcut.- Parameters:
path (str) – The original path to expand.
- Returns:
The fully expanded, absolute path to the given
path
- Return type:
Example
from cement.utils import fs fs.abspath('~/some/path') fs.abspath('./some.file')
- cement.utils.fs.backup(path: str, suffix: str = '.bak', **kwargs: Any) str | None [source]
Rename a file or directory safely without overwriting an existing backup of the same name.
- Parameters:
- Keyword Arguments:
- Returns:
The new path of backed up file/directory
- Return type:
Example
from cement.utils import fs fs.backup('/path/to/original/file')
- cement.utils.fs.ensure_dir_exists(path: str) None [source]
Ensure the directory
path
exists, and if not create it.- Parameters:
path (str) – The filesystem path of a directory.
- Raises:
AssertionError – If the directory
path
exists, but is not adirectory. –
Returns: None
- cement.utils.fs.ensure_parent_dir_exists(path: str) None [source]
Ensure the parent directory of
path
(file, or directory) exists, and if not create it.- Parameters:
path (str) – The filesystem path of a file or directory.
Returns: None
- cement.utils.fs.join(*args: str, **kwargs: Any) str [source]
Return a complete, joined path, by first calling
abspath()
on the first item to ensure the final path is complete.- Parameters:
paths (list) – A list of paths to join together.
- Returns:
The complete and absolute joined path.
- Return type:
Example
from cement.utils import fs fs.join('~/some/path', 'some/other/relevant/paht')