ahvn.utils.basic.file_utils module

ahvn.utils.basic.file_utils.touch_file(path, encoding=None, clear=False, content=None)[源代码]

Create an empty file at the specified path, or write content to it.

警告

An extra newline will be added at the end of the string to be consistent with the behavior of save_txt and append_txt.

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

  • encoding (str, optional) -- Encoding to use for the file. Defaults to None.

  • clear (bool, optional) -- If True, clears the file if it exists. If False, do nothing if the file exists. Defaults to False.

  • content (str, optional) -- Content to write to the file (overwrite existing content). Only works if ckear is True or the file does not exist. Notice that an empty string content="" is different from content=None, the former writes a linebreak to the file, while the latter creates an empty file. Defaults to None, which creates an empty file.

返回:

The path to the created or cleared file.

返回类型:

path (str)

抛出:

ValueError -- If the path exists and is not a file.

ahvn.utils.basic.file_utils.touch_dir(path, clear=False)[源代码]

Create an empty directory at the specified path.

参数:
  • path (str) -- The directory path to create.

  • clear (bool, optional) -- If True, clears the directory if it exists. If False, do nothing if the directory exists. Defaults to False.

返回:

The path to the created or cleared directory.

返回类型:

path (str)

ahvn.utils.basic.file_utils.exists_file(path)[源代码]

Check if a file exists at the specified path.

参数:

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

返回:

True if the file exists, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.exists_dir(path)[源代码]

Check if a directory exists at the specified path.

参数:

path (str) -- The directory path to check.

返回:

True if the directory exists, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.exists_path(path)[源代码]

Check if a path exists at the specified path.

参数:

path (str) -- The path to check.

返回:

True if the path exists, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.list_files(path, ext=None, abs=False)[源代码]

List all files in the specified directory with optional extension filtering. Guarantees an alphabetical sorted order (case-insensitive) of files.

参数:
  • path (str) -- The directory path to list files from.

  • ext (Union[str, List[str]]) -- The extension to check for. If None, return all files. 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 should be a string, which will check for that specific extension (preferably without a dot). Repeated extensions does NOT result in repeated files.

  • abs (bool, optional) -- If True, return absolute paths. Defaults to False.

生成器:

str -- The file paths that match the criteria.

返回类型:

Generator[str, None, None]

ahvn.utils.basic.file_utils.list_dirs(path, abs=False)[源代码]

List all directories in the specified directory. Guarantees an alphabetical sorted order (case-insensitive) of directories.

参数:
  • path (str) -- The directory path to list directories from.

  • abs (bool, optional) -- If True, return absolute paths. Defaults to False.

生成器:

str -- The directory paths.

返回类型:

Generator[str, None, None]

ahvn.utils.basic.file_utils.list_paths(path, ext=None, abs=False)[源代码]

List all directories and files in the specified directory with optional extension filtering. Directories are always listed before files. Guarantees an alphabetical sorted order (case-insensitive) of files.

参数:
  • path (str) -- The directory path to list paths from.

  • ext (Union[str, List[str]]) -- The extension to check for. If None, return all files. 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 should be a string, which will check for that specific extension (preferably without a dot). Repeated extensions does NOT result in repeated files.

  • abs (bool, optional) -- If True, return absolute paths. Defaults to False.

生成器:

str -- The file and directory paths that match the criteria.

返回类型:

Generator[str, None, None]

ahvn.utils.basic.file_utils.enum_files(path, ext=None, abs=False)[源代码]

Enumerate all files in the specified directory with optional extension filtering. Guarantees an alphabetical sorted order (case-insensitive) of files.

参数:
  • path (str) -- The directory path to enumerate files from.

  • ext (Union[str, List[str]]) -- The extension to check for. If None, return all files. 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 should be a string, which will check for that specific extension (preferably without a dot). Repeated extensions does NOT result in repeated files.

  • abs (bool, optional) -- If True, return absolute paths. Defaults to False.

生成器:

str -- The file path and its base name.

返回类型:

Generator[str, None, None]

ahvn.utils.basic.file_utils.enum_dirs(path, abs=False)[源代码]

Enumerate all directories in the specified directory. Guarantees an alphabetical sorted order (case-insensitive) of directories.

参数:
  • path (str) -- The directory path to enumerate directories from.

  • abs (bool, optional) -- If True, return absolute paths. Defaults to False.

生成器:

str -- The directory path.

返回类型:

Generator[str, None, None]

ahvn.utils.basic.file_utils.enum_paths(path, ext=None, abs=False)[源代码]

Enumerate all directories and files in the specified directory with optional extension filtering. Directories are always listed before files. Guarantees an alphabetical sorted order (case-insensitive) of files.

参数:
  • path (str) -- The directory path to enumerate paths from.

  • ext (Union[str, List[str]]) -- The extension to check for. If None, return all files. 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 should be a string, which will check for that specific extension (preferably without a dot). Repeated extensions does NOT result in repeated files.

  • abs (bool, optional) -- If True, return absolute paths. Defaults to False.

生成器:

str -- The directory or file path.

返回类型:

Generator[str, None, None]

ahvn.utils.basic.file_utils.empty_dir(path, ext=None)[源代码]

Check if a directory exists and is empty. If ext is provided, checks if there are any files with the specified extension(s) in the directory (recursively).

参数:
  • path (str) -- The directory path to check.

  • ext (Union[str, List[str]]) -- The extension to check for. If None, return all files. 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 should be a string, which will check for that specific extension (preferably without a dot). Repeated extensions does NOT result in repeated files.

返回:

True if the directory exists and contains no items, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.nonempty_dir(path, ext=None)[源代码]

Check if a directory exists and is not empty. If ext is provided, checks if there are any files with the specified extension(s) in the directory (recursively).

参数:
  • path (str) -- The directory path to check.

  • ext (Union[str, List[str]]) -- The extension to check for. If None, return all files. 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 should be a string, which will check for that specific extension (preferably without a dot). Repeated extensions does NOT result in repeated files.

返回:

True if the directory exists and contains at least one item, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.copy_file(src, dst, mode='replace')[源代码]

Copy a file from source to destination.

参数:
  • src (str) -- The source file path.

  • dst (str) -- The destination file path.

  • mode (str, optional) -- The copy mode. 'replace' to overwrite, 'skip' to skip if exists, 'strict' to raise an error if exists. Defaults to 'replace'.

返回:

The destination file path.

返回类型:

str

抛出:

ValueError -- If the source does not exist or is not a file, or if the destination exists and mode is 'strict'.

ahvn.utils.basic.file_utils.copy_dir(src, dst, mode='merge', **kwargs)[源代码]

Copy a directory from source to destination.

参数:
  • src (str) -- The source directory path.

  • dst (str) -- The destination directory path.

  • mode (str, optional) -- The copy mode. 'replace' to overwrite, 'skip' to skip if exists, 'strict' to raise an error if exists, 'merge' to merge contents. Defaults to 'merge'.

  • **kwargs -- Additional arguments to pass to shutil.copytree().

返回:

The destination directory path.

返回类型:

str

抛出:

ValueError -- If the source does not exist or is not a directory, or if the destination exists and mode is 'strict'.

ahvn.utils.basic.file_utils.copy_path(src, dst, mode='merge', **kwargs)[源代码]

Copy a path from source to destination, handling both files and directories.

参数:
  • src (str) -- The source path.

  • dst (str) -- The destination path.

  • mode (str, optional) -- The copy mode. 'replace' to overwrite, 'skip' to skip if exists, 'strict' to raise an error if exists, 'merge' to merge contents. Defaults to 'merge'.

  • **kwargs -- Additional arguments to pass to shutil.copytree().

返回:

The destination path.

返回类型:

str

抛出:

ValueError -- If the source does not exist or is not a file/directory, or if the destination exists and mode is 'strict'.

ahvn.utils.basic.file_utils.delete_file(path)[源代码]

Delete a file at the specified path.

参数:

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

返回:

True if the file exists and was deleted, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.delete_dir(path)[源代码]

Delete a directory at the specified path.

参数:

path (str) -- The directory path to delete.

返回:

True if the directory exists and was deleted, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.delete_path(path)[源代码]

Delete a path at the specified path.

参数:

path (str) -- The path to delete.

返回:

True if the path exists and was deleted, False otherwise.

返回类型:

bool

ahvn.utils.basic.file_utils.folder_diagram(path, annotations=None, name=None, limit=16)[源代码]

Build a tree diagram from a directory path or serialized data.

Creates a hierarchical tree structure showing the file/folder organization with optional annotations. This format is optimized for LLM understanding of the resource structure.

参数:
  • path (str) -- Directory path to build tree from.

  • annotations (Optional[Dict[str, str]]) -- File-level annotations to display.

  • name (Optional[str]) -- Custom label for the root node. Defaults to the basename of path.

  • limit (int, optional) -- Maximum number of entries to render per directory before collapsing the middle section. Defaults to 16, displaying at most 16 directories and 16 files per directory. It is recommended to set this to an even number.

返回:

Formatted tree structure diagram with optional annotations.

返回类型:

str

示例

>>> folder_diagram("/path/to/project", {"src/main.py": "Main entry point"})
'''
project/
├── file1.py
└── src/
    └── main.py  # Main entry point
'''