cement.utils.fs
¶
Common File System Utilities.
-
class
cement.utils.fs.
Tmp
(**kwargs)[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')
- cleanup (bool) – Whether or not to delete the temporary directory and
file on exit (when used with the
-
cement.utils.fs.
abspath
(path, strip_trailing_slash=True)[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: str Example
from cement.utils import fs fs.abspath('~/some/path') fs.abspath('./some.file')
-
cement.utils.fs.
backup
(path, suffix='.bak')[source]¶ Rename a file or directory safely without overwriting an existing backup of the same name.
Parameters: 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)[source]¶ Ensure the directory
path
exists, and if not create it.Parameters: path (str) – The filesystem path of a directory.
Raises: AssertionError
– If the directorypath
exists, but is not adirectory.
Returns: None
-
cement.utils.fs.
ensure_parent_dir_exists
(path)[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, **kwargs)[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: list Example
from cement.utils import fs fs.join('~/some/path', 'some/other/relevant/paht')
-
cement.utils.fs.
join_exists
(*paths)[source]¶ Wrapper around
os.path.join()
,os.path.abspath()
, andos.path.exists()
.Parameters: paths (list) – List of paths to join, and then return True
if that path exists, orFalse
if it does not.Returns: - First item is the fully joined absolute path, and the second
- is
bool
(whether that path exists or not).
Return type: tuple