fileutils

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
  • 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

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
  • path – File to delete

  • remove – Optional function to remove passed path

oslo_utils.fileutils.ensure_tree(path, mode=511)

Create a directory (and any ancestor directories required)

Parameters
  • path – Directory to create

  • mode – Directory creation permissions

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
  • path – The file path to read

  • num – The number of bytes to return

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
  • path – File to work with

  • remove – Optional function to remove passed path

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
  • 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.