npu.inout.io_utils

Classes

IOUtils()

class npu.inout.io_utils.IOUtils
Author:

Alberto M. Esmoris Pena

Class with util static methods for general input/output operations.

static make_directory(dirpath, recursive=True, error_if_exists=False)

Create the given directory.

Parameters:
  • dirpath (str) – The path of the directory to be created.

  • recursive (bool) – Whether to make parent directories recursively (True, default) or not (False).

  • error_if_exists – Whether to raise an exception if the directory already exists (True) or not (False, default).

Returns:

Nothing at all, but the directory is created at the given path.

static download_from_url(url, chunk_size=67108864)

Read/download the bytes representing a file from a given URL.

Parameters:
  • url (str) – The URL corresponding to the file whose bytes must be downloaded.

  • chunk_size (float) – The chuynk size for each reading operation on the input stream (in Bytes, def. 64 MiB).

Returns:

The bytes representing the downloaded file.

Return type:

bytes

static remove_file(path, check_file_exists=False)

Remove/delete the file at the given path.

Parameters:
  • path (str) – Path of the file to be deleted.

  • check_file_exists (bool) – If True, the file will only be removed provided it exists. This can prevent errors due to trying to delete a non-existent file.

Returns:

Nothing at all.

static validate_path_to_file(path, msg='Cannot find file at given path:', accept_url=False)

Validate the given path points to an accessible file.

Parameters:
  • path (str) – Path to a file that must be validated.

  • msg (str) – Message for the exception.

  • accept_url (bool) – If True, when the given path is a valid and accessible URL it will be accepted as a valid path. If False, URLs will not be accepted.

Returns:

Nothing but an exception will be raised if the path is not valid.

static validate_path_to_directory(path, msg='Given path does not point to an accessible directory:', try_to_make=True)

Validate the given path points to an accessible directory.

Parameters:
  • path (str) – Path to a directory that must be validated.

  • msg (str) – Message for the exception.

  • try_to_make (bool) – Whether try to make the given directory (True) or just validate it exists (False).

Returns:

Nothing but an exception will be raised if the path is not valid.

static validate_url_to_file(url, msg='Given URL is not accessible:')

Validate the given URL can be accessed.

Parameters:
  • url (str) – The URL to be checked.

  • msg (str) – Message for the exception.

Returns:

Nothing but an exception will be raised if the URL is not accessible.

static is_url(s)

Check whether the given string represents a URL.

Parameters:

s – The string to be checked.

Returns:

True if the given string represents a URL, false otherwise.

static does_file_exist(path)

Check whether the given path represents a file that exists in the filesystem (True) or not (False).

Parameters:

path – Path to the file whose existence must be checked.

Returns:

True if the file at the given path exists (as a file), False otherwise.

Return type:

bool

static is_single_line_file(path)

Check whether the given path represents a single-line file.

A file is said to be single line either if it is empty, if it only has one line or if it has two lines but the second one is empty (i.e., does not have any character).

Parameters:

path (str) – Path to the file to be checked

Returns:

True if the given path points to a single-line file, False otherwise.

Return type:

bool