Source code for langchain_community.document_loaders.python

import tokenize
from pathlib import Path
from typing import Union

from langchain_community.document_loaders.text import TextLoader


[docs]class PythonLoader(TextLoader): """加载`Python`文件,遵循指定的非默认编码。"""
[docs] def __init__(self, file_path: Union[str, Path]): """使用文件路径进行初始化。 参数: file_path:要加载的文件的路径。 """ with open(file_path, "rb") as f: encoding, _ = tokenize.detect_encoding(f.readline) super().__init__(file_path=file_path, encoding=encoding)