File utilities.
New in version 1.8.
oslo_utils.fileutils.
compute_file_checksum
(path, read_chunksize=65536, algorithm='sha256')¶Compute checksum of a file’s contents.
Parameters: |
|
---|---|
Returns: | Hex digest string of the checksum |
New in version 3.31.0.
oslo_utils.fileutils.
delete_if_exists
(path, remove=<built-in function unlink>)¶Delete a file, but ignore file not found error.
Parameters: |
|
---|
oslo_utils.fileutils.
ensure_tree
(path, mode=511)¶Create a directory (and any ancestor directories required)
Parameters: |
|
---|
oslo_utils.fileutils.
is_json
(file_path)¶Check if file is of json type or not.
This function try to load the input file using json.loads() and return False if ValueError otherwise True.
Parameters: | file_path – The file path to check |
---|---|
Returns: | bool |
oslo_utils.fileutils.
is_yaml
(file_path)¶Check if file is of yaml type or not.
This function try to load the input file using yaml.safe_load() and return True if loadable. Because every json file can be loadable in yaml, so this function return False if file is loadable using json.loads() means it is json file.
Parameters: | file_path – The file path to check |
---|---|
Returns: | bool |
oslo_utils.fileutils.
last_bytes
(path, num)¶Return num bytes from the end of the file and unread byte count.
Returns a tuple containing some content from the file and the
number of bytes that appear in the file before the point at which
reading started. The content will be at most num
bytes, taken
from the end of the file. If the file is smaller than num
bytes the entire content of the file is returned.
Parameters: |
|
---|---|
Returns: | (data, unread_bytes) |
oslo_utils.fileutils.
remove_path_on_error
(path, remove=<function delete_if_exists>)¶Protect code that wants to operate on PATH atomically. Any exception will cause PATH to be removed.
Parameters: |
|
---|
oslo_utils.fileutils.
write_to_tempfile
(content, path=None, suffix='', prefix='tmp')¶Create a temporary file containing data.
Create a temporary file containing specified content, with a specified filename suffix and prefix. The tempfile will be created in a default location, or in the directory path, if it is not None. path and its parent directories will be created if they don’t exist.
Parameters: |
|
---|
For example: it can be used in database tests for creating configuration files.
New in version 1.9.
Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.