ahvn.utils.basic.path_utils module¶

ahvn.utils.basic.path_utils.pj(*args, abs=False)[source]¶

Join a list of strings into a path. Platform-agnostic and user-expanding. Spaces and trailing slashes are stripped from each argument.

Parameters:
  • *args (List[str]) – Components of the path to join. Each argument should be a string.

  • abs (bool, optional) – If True, returns the absolute path. Defaults to False.

Returns:

The joined, normalized path. Expands ‘~’ to the user’s home directory.

Return type:

str

ahvn.utils.basic.path_utils.get_file_ext(path)[source]¶

Get the file extension (without a dot) for a specified path.

Parameters:

path (str) – The path to the file.

Returns:

The file extension without a dot.

Return type:

str

ahvn.utils.basic.path_utils.get_file_name(path, ext=True, abs=False)[source]¶

Get the full file name from the specified path. When a file extension is provided (preferrably without a dot), it will be used instead of the file’s original extension.

Parameters:
  • path (str) – str: The path to the file.

  • ext (Union[bool, str]) – Union[bool, str]: If True, returns the file name with its original extension. If False, returns the file name without any extension. If a string is provided, it will be used as the new extension.

  • abs (bool) – bool: If True, returns the absolute path. Defaults to False.

Returns:

The file name with or without the specified extension.

Return type:

str

Examples

>>> get_file_name("A/B/C/file.txt")
'A/B/C/file.txt'
>>> get_file_name("A/B/C/file.txt", ext=True)
'A/B/C/file.txt'
>>> get_file_name("A/B/C/file.txt", ext=False)
'A/B/C/file'
>>> get_file_name("A/B/C/file.txt", ext="md")
'A/B/C/file.md'
>>> get_file_name("A/B/C/")
'C'
ahvn.utils.basic.path_utils.get_file_basename(path, ext=True)[source]¶

Get the base name of the file from the specified path. When a file extension is provided (preferably without a dot), it will be used instead of the file’s original extension.

Parameters:
  • path (str) – The path to the file.

  • ext (Union[bool, str]) – If True, returns the file name with its original extension. If False, returns the file name without any extension. If a string is provided, it will be used as the new extension.

Returns:

The base name of the file.

Return type:

str

Examples

>>> get_file_basename("A/B/C/file.txt")
'file.txt'
>>> get_file_basename("A/B/C/file.txt", ext=True)
'file.txt'
>>> get_file_basename("A/B/C/file.txt", ext=False)
'file'
>>> get_file_basename("A/B/C/file.txt", ext="md")
'file.md'
ahvn.utils.basic.path_utils.get_file_dir(path, abs=False)[source]¶

Get the directory of the specified file path.

Parameters:
  • path (str) – The path to the file.

  • abs (bool) – If True, returns the absolute path. Defaults to False.

Returns:

The directory of the file.

Return type:

str

Examples

>>> get_file_dir("A/B/C/file.txt")
'A/B/C'
>>> get_file_dir("/")
'/'
ahvn.utils.basic.path_utils.has_file_ext(path, ext=None)[source]¶

Check if the specified file path has a given extension or any of the extensions in a list.

Parameters:
  • path (str) – The path to the file.

  • ext (Union[None, str, List[str]]) – The extension to check for. If None, return whether the file has any extension. If a string, checks for that specific extension. A string may contain multiple extensions separated by commas or semicolons, which will be split into a list. If a list, checks if the file has any of the extensions in the list. Each list item may be None, which will check if the file has NO extension, or a string, which will check for that specific extension (preferably without a dot).

Returns:

True if the file has the specified extension(s), False otherwise.

Return type:

bool

Examples

>>> has_file_ext("A/B/C/file.txt", ext="txt")
True
>>> has_file_ext("A/B/C/file.txt", ext="md")
False
>>> has_file_ext("A/B/C/file.txt", ext=["txt", "md"])
True
>>> has_file_ext("A/B/C/file.txt", ext=None)
True
>>> has_file_ext("A/B/C/file", ext=None)
False
>>> has_file_ext("A/B/C/file", ext=[None, "txt,md;py"])
True