Common File System Utilities.
Provides creation and cleanup of a temporary directory, and file.
- cleanup (bool) – Whether or not to delete the temporary directory and
files on exit (when used with the
- 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 will be created in (note that the temporary file is already created under the temporary directory provided here). Default: system default temporary path
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')
Remove the temporary directory (and file) if it exists, and
- cleanup (bool) – Whether or not to delete the temporary directory and files on exit (when used with the
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
Return type: str
from cement.utils import fs fs.abspath('~/some/path') fs.abspath('./some.file')
Rename a file or directory safely without overwriting an existing backup of the same name.
The new path of backed up file/directory
from cement.utils import fs fs.backup('/path/to/original/file')
Ensure the directory
pathexists, and if not create it.
path (str) – The filesystem path of a directory.
AssertionError– If the directory
pathexists, but is not a
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.
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
from cement.utils import fs fs.join('~/some/path', 'some/other/relevant/paht')
Parameters: paths (list) – List of paths to join, and then return
Trueif that path exists, or
Falseif it does not.
- First item is the fully joined absolute path, and the second
bool(whether that path exists or not).
Return type: tuple