File utilities.
New in version 1.8.
Compute checksum of a file’s contents.
path – Path to the file
read_chunksize – Maximum number of bytes to be read from the file at once. Default is 65536 bytes or 64KB
algorithm – The hash algorithm name to use. For example, ‘md5’, ‘sha256’, ‘sha512’ and so on. Default is ‘sha256’. Refer to hashlib.algorithms_available for available algorithms
Hex digest string of the checksum
New in version 3.31.0.
Delete a file, but ignore file not found error.
path – File to delete
remove – Optional function to remove passed path
Create a directory (and any ancestor directories required)
path – Directory to create
mode – Directory creation permissions
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.
file_path – The file path to check
bool
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.
file_path – The file path to check
bool
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.
path – The file path to read
num – The number of bytes to return
(data, unread_bytes)
Protect code that wants to operate on PATH atomically. Any exception will cause PATH to be removed.
path – File to work with
remove – Optional function to remove passed path
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.
content – bytestring to write to the file
path – same as parameter ‘dir’ for mkstemp
suffix – same as parameter ‘suffix’ for mkstemp
prefix – same as parameter ‘prefix’ for mkstemp
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.