ahvn.utils.basic.path_utils module

ahvn.utils.basic.path_utils.pj(*args, abs=False)[源代码]

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

参数:
  • *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.

返回:

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

返回类型:

str

ahvn.utils.basic.path_utils.get_file_ext(path)[源代码]

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

参数:

path (str) -- The path to the file.

返回:

The file extension without a dot.

返回类型:

str

ahvn.utils.basic.path_utils.get_file_name(path, ext=True, abs=False)[源代码]

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.

参数:
  • 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.

返回:

The file name with or without the specified extension.

返回类型:

str

示例

>>> 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)[源代码]

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.

参数:
  • 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.

返回:

The base name of the file.

返回类型:

str

示例

>>> 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)[源代码]

Get the directory of the specified file path.

参数:
  • path (str) -- The path to the file.

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

返回:

The directory of the file.

返回类型:

str

示例

>>> 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)[源代码]

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

参数:
  • 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).

返回:

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

返回类型:

bool

示例

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