当前位置:首页>python>【极致详解Python 文件管理四大核心库】:os、glob、shutil、pathlib详细使用指南

【极致详解Python 文件管理四大核心库】:os、glob、shutil、pathlib详细使用指南

  • 2026-06-28 10:04:00
【极致详解Python 文件管理四大核心库】:os、glob、shutil、pathlib详细使用指南

文章以 osos.pathglobshutilpathlib 库为主线,对每个函数/方法的参数、返回值、注意事项、典型用法进行详细拆解,力求让你看完之后全面掌握文件处理操作

一、os 模块 – 操作系统接口

1.1 目录操作

os.getcwd()

os.getcwd() -> str
  • 作用:返回当前工作目录的绝对路径字符串
  • 注意:该路径是 Python 进程启动时的工作目录,不一定是脚本所在目录。
  • 示例
import osprint(os.getcwd())

os.chdir(path)

os.chdir(path: Union[str, bytes, PathLike]) -> None
  • 作用:将当前工作目录更改为 path,类似于 shell 的 cd 命令。
  • 参数path – 目标目录路径(字符串或 bytes 或 path-like 对象)。
  • 异常FileNotFoundError(路径不存在)、NotADirectoryError(路径不是目录)、PermissionError(无权限)。
  • 示例
os.chdir("/tmp")
print(os.getcwd())

os.listdir(path='.')

os.listdir(path: Union[str, bytes, PathLike] = '.') -> List[str]
  • 作用:返回指定目录下所有条目名称(不含完整路径)的列表,不包含. (当前目录)和 ..上一级目录)。
  • 参数path – 目录路径,默认为当前目录。
  • 注意:返回的列表顺序不确定(通常按文件系统内部顺序)。
  • 示例
entries = os.listdir("/home/user")

os.mkdir(path, mode=0o777, *, dir_fd=None)

os.mkdir(path: Union[str, bytes, PathLike], mode: int = 0o777, *, dir_fd: Optional[int] = None) -> None
  • 作用:创建单级目录。父目录必须已经存在,否则抛出 FileNotFoundError
  • 参数
    • mode – 权限模式(八进制),仅在 Unix 有效,Windows 忽略。
    • dir_fd – 相对路径的描述符。
  • 异常FileExistsError – 目录已存在;FileNotFoundError – 父目录不存在。
  • 示例
os.mkdir("new_folder")   # 创建 new_folderos.mkdir("/root/secret", mode=0o700)  # 仅所有者可读写执行

os.makedirs(path, mode=0o777, exist_ok=False)

os.makedirs(name: Union[str, bytes, PathLike], mode: int = 0o777, exist_ok: bool = False) -> None
  • 作用递归创建多级目录,类似 mkdir -p
  • 参数
    • exist_ok=False ,如果为 True,当目标目录已存在时不抛出异常;如果为 False 且目录存在则抛出 FileExistsError
  • 示例
# 创建 a/b/c 三级目录,父目录不存在也会自动创建os.makedirs("a/b/c", exist_ok=True)

os.rmdir(path, *, dir_fd=None)

os.rmdir(path: Union[str, bytes, PathLike], *, dir_fd: Optional[int] = None) -> None
  • 作用:删除空目录。如果目录非空,抛出 OSError(错误码 39 "Directory not empty")。
  • 注意:要删除非空目录,请使用 shutil.rmtree()
  • 示例
os.rmdir("empty_folder")  # 成功os.rmdir("non_empty")   # OSError

os.removedirs(name)

os.removedirs(name: Union[str, bytes, PathLike]) -> None
  • 作用:递归删除目录,从给定的路径开始自底向上删除空目录,遇到非空目录即停止。
  • 示例
# 假设目录结构:a/b/c,且 c 为空os.removedirs("a/b/c")  

os.walk(top, topdown=True, onerror=None, followlinks=False)

os.walk(top: Union[str, bytes, PathLike], topdown: bool = True, onerror: Optional[Callable] = None, followlinks: bool = False) -> Iterator[Tuple[str, List[str], List[str]]]
  • 作用:遍历目录树,生成三元组 (dirpath, dirnames, filenames)
  • 参数
    • topdown=True – 若为 True,先遍历顶层目录再进入子目录;若为 False,先进入子目录再返回顶层(后序遍历)。
    • onerror – 当访问目录出错时的回调函数,默认抛出异常。
    • followlinks=False – 若为 True,会跟随符号链接(可能导致循环链接无限递归)。
  • 返回值:生成器,每次迭代返回 (dirpath, dirnames, filenames)
    • dirpath – 当前遍历的目录路径(字符串)。
    • dirnames – 该目录下的子目录名称列表(可修改,用于控制遍历)。
    • filenames – 该目录下的非目录文件名称列表。
  • 经典用法
for root, dirs, files in os.walk("/home/user"):for file in files:if file.endswith(".py"):            print(os.path.join(root, file))

1.2 文件操作

os.remove(path, *, dir_fd=None)

os.unlink(path, *, dir_fd=None)

os.remove(path: Union[str, bytes, PathLike], *, dir_fd: Optional[int] = None) -> None
  • 作用:删除文件(不能删除目录)。两个函数完全等价。
  • 异常IsADirectoryError – 试图删除目录;FileNotFoundError – 文件不存在。
  • 示例
os.remove("temp.txt")

os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)

os.rename(src: Union[str, bytes, PathLike], dst: Union[str, bytes, PathLike], *, src_dir_fd: Optional[int] = None, dst_dir_fd: Optional[int] = None) -> None
  • 作用:重命名文件或目录。也可用于移动
  • 注意:如果 dst 已存在,Unix 下会覆盖;Windows 下抛出 FileExistsError
  • 跨设备移动会失败,此时应使用 shutil.move()
  • 示例
os.rename("old.txt""new.txt")  # 重命名os.rename("file.txt""subdir/file.txt")  # 移动(同一设备)

os.stat(path, *, dir_fd=None, follow_symlinks=True)

os.stat(path: Union[str, bytes, PathLike], *, dir_fd: Optional[int] = None, follow_symlinks: bool = True) -> os.stat_result
  • 作用:获取文件/目录的元数据,返回 stat_result 对象(类元组)。
  • 常用属性
    属性
    含义
    示例值
    st_size
    文件大小(字节)
    1024
    st_mtime
    最后修改时间(时间戳)
    1678901234.567
    st_ctime
    元数据变更时间(Unix)或创建时间(Windows)
    1678901234.567
    st_atime
    最后访问时间
    1678901234.567
    st_mode
    文件类型与权限(掩码)
    33188 (0o100644)
    st_uid
    所有者用户 ID
    1000
  • 示例
info = os.stat("data.bin")print(f"大小: {info.st_size} 字节, 修改时间: {info.st_mtime}")

1.3 os.path 子模块 – 路径字符串处理

os.path.exists(path)

os.path.exists(path: Union[str, bytes, PathLike]) -> bool
  • 作用:判断路径(文件或目录)是否存在。
  • 注意:对失效的符号链接返回 False

os.path.isfile(path) / os.path.isdir(path)

os.path.isfile(path: Union[str, bytes, PathLike]) -> boolos.path.isdir(path: Union[str, bytes, PathLike]) -> bool
  • 作用:判断是否为普通文件/目录。如果路径是符号链接,会解析链接后判断。
  • 注意:路径不存在时返回 False

os.path.join(a, *paths)

os.path.join(a: Union[str, bytes, PathLike], *paths: Union[str, bytes, PathLike]) -> str
  • 作用:智能拼接路径,使用当前操作系统的路径分隔符(Windows 用 \,其他用 /)。
  • 规则
    • 如果某个参数是绝对路径,则它之前的所有参数被丢弃。
    • 空字符串保留为当前目录(即 .)。
  • 示例
os.path.join("home""user""data.txt")  # 'home/user/data.txt' (Linux)os.path.join("C:""Users""file.txt")   # 'C:/Users/file.txt' (Windows)

os.path.dirname(path) / os.path.basename(path)

os.path.dirname(path: Union[str, bytes, PathLike]) -> stros.path.basename(path: Union[str, bytes, PathLike]) -> str
  • 作用:返回路径的目录部分/文件名部分。
  • 示例
p = "/home/user/file.txt"os.path.dirname(p)   # '/home/user'os.path.basename(p)  # 'file.txt'

os.path.splitext(path)

os.path.splitext(path: Union[str, bytes, PathLike]) -> Tuple[str, str]
  • 作用:分离文件名和扩展名(最后一个点之后的部分)。
  • 注意:以 . 开头的文件(如 .bashrc)被视为无扩展名。
  • 示例
os.path.splitext("archive.tar.gz")  # ('archive.tar', '.gz')os.path.splitext(".hidden")         # ('.hidden', '')

os.path.abspath(path)

os.path.abspath(path: Union[str, bytes, PathLike]) -> str
  • 作用:返回规范化后的绝对路径(解析 ...,不解析符号链接)。
  • 示例
os.path.abspath("docs/../readme.md")  # '/current/working/dir/readme.md'

os.path.getsize(path)

os.path.getsize(path: Union[str, bytes, PathLike]) -> int
  • 作用:返回文件大小(字节)。对目录的行为依赖于系统(通常返回 0 或 4096,不建议用于目录)。

1.4 环境变量与进程

os.environ

os.environ : os._Environ
  • 本质:一个类似字典的对象,包含当前进程的环境变量。
  • 常用操作
# 获取(不存在返回 None,推荐 get)path = os.environ.get("PATH""/usr/bin")# 设置(仅影响当前进程及子进程)os.environ["MY_VAR"] = "value"# 删除del os.environ["TEMP"]

os.system(command)

os.system(command: str) -> int
  • 作用:在子 shell 中执行命令,返回退出状态码(0 表示成功)。
  • 警告:不安全(易受 shell 注入攻击),不推荐使用。建议使用 subprocess.run()
  • 示例
os.system("ls -l")  # 显示文件列表,返回 0

二、glob 模块 – Unix 风格路径模式匹配

glob.glob(pathname, *, recursive=False)

glob.glob(pathname: str, *, recursive: bool = False) -> List[str]
  • 作用:返回匹配 pathname 的所有路径列表,结果未排序(依赖系统)。
  • 参数
    • pathname – 包含通配符的路径模式。
    • recursive=False – 若为 True,则 ** 匹配任意文件及零个或多个目录/子目录。
  • 通配符详解
    符号
    含义
    示例
    匹配结果
    *
    任意字符(0 个或多个,不含路径分隔符)
    *.txta.txt
    b.txt
    **
    递归匹配所有目录(需 recursive=True
    **/*.pya.py
    sub/b.pysub/sub2/c.py
    ?
    单个任意字符
    data?.csvdata1.csv
    dataA.csv
    [seq]
    匹配序列中的任意字符
    [abc]*.txta.txt
    b.txtc.txt
    [!seq]
    匹配不在序列中的字符
    [!0-9]*.txta.txt
    , 但不是 1.txt
  • 注意glob.glob 默认不匹配以点开头的文件(如 .bashrc),除非模式显式以点开头。

glob.iglob(pathname, *, recursive=False)

glob.iglob(pathname: str, *, recursive: bool = False) -> Iterator[str]
  • 作用:与 glob 相同,但返回迭代器而非列表,适合处理海量文件(内存友好)。
  • 示例
for file in glob.iglob("logs/*.log"):    process(file)  # 逐个处理,不一次性加载所有路径

glob.escape(pathname)

glob.escape(pathname: str) -> str
  • 作用:转义模式中的特殊字符(*?[]),使其被当作普通字符匹配。
  • 示例
pattern = glob.escape("a[b].txt")  # 'a\\[b\\].txt'glob.glob(pattern)  # 只匹配字面量 "a[b].txt"

glob.translate(pathname)

glob.translate(pathname: str) -> str
  • 作用:将 glob 模式转换为正则表达式(可用于 re 模块进一步匹配)。
  • 示例
regex = glob.translate("*.txt")print(regex)  # '.*\\.txt\\Z(?ms)'

三、shutil 模块 – 高级文件操作

3.1 复制操作

shutil.copy(src, dst, *, follow_symlinks=True)

shutil.copy(src: Union[str, bytes, PathLike], dst: Union[str, bytes, PathLike], *, follow_symlinks: bool = True) -> str
  • 作用:复制文件 src 到 dst,保留文件权限模式(但不保留元数据如修改时间)。
  • 参数
    • dst – 可以是文件名(重命名)或目录名(保留原文件名复制到该目录)。
    • follow_symlinks=False – 复制符号链接本身而不是指向的文件。
  • 返回值:返回目标路径字符串。
  • 示例
shutil.copy("source.txt""dest.txt")          # 复制并重命名shutil.copy("source.txt""backup_dir/")       # 复制到目录,保留原文件名

shutil.copy2(src, dst, *, follow_symlinks=True)

shutil.copy2(src, dst, *, follow_symlinks=True) -> str
  • 作用:与 copy 类似,但尽量保留元数据(修改时间、访问时间等)。这是 copy + copystat 的组合。

shutil.copyfile(src, dst, *, follow_symlinks=True)

shutil.copyfile(src: Union[str, bytes, PathLike], dst: Union[str, bytes, PathLike], *, follow_symlinks: bool = True) -> str
  • 作用:仅复制文件内容(不含权限、元数据)。如果 dst 已存在,会被覆盖。
  • 要求src 和 dst 必须是文件(不能是目录)。

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False)

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False) -> str
  • 作用:递归复制整个目录树。
  • 关键参数
    • symlinks=False – 若为 True,复制符号链接本身;若为 False,跟随链接复制其指向的文件。
    • ignore – 可调用对象,用于排除文件/目录,如 ignore=shutil.ignore_patterns("*.tmp", "*.log")
    • copy_function – 默认 copy2,可替换为 copy 以加快速度。
    • dirs_exist_ok=False – 若为 True,允许目标目录存在(Python 3.8+)。
  • 示例
shutil.copytree("source_dir""dest_dir", ignore=shutil.ignore_patterns("__pycache__""*.pyc"))

3.2 移动、删除与磁盘

shutil.move(src, dst, copy_function=copy2)

shutil.move(src: Union[str, bytes, PathLike], dst: Union[str, bytes, PathLike], copy_function: Callable = copy2) -> str
  • 作用:移动文件或目录到新位置。
  • 实现逻辑
    1. 先尝试 os.rename()(同一文件系统内是原子的)。
    2. 如果失败(跨设备),则用 copy_function 复制内容,然后删除源。
  • 返回值:目标路径字符串。

shutil.rmtree(path, ignore_errors=False, onerror=None, *, onexc=None)

shutil.rmtree(path: Union[str, bytes, PathLike], ignore_errors: bool = False, onerror: Optional[Callable] = None, *, onexc: Optional[Callable] = None) -> None
  • 作用:递归删除整个目录树(包括所有子目录和文件),不可逆
  • 参数
    • ignore_errors=True – 忽略删除过程中的错误。
    • onexc – 错误处理回调函数(Python 3.12+),接收 (func, path, excinfo)
  • 示例
shutil.rmtree("temp_dir")  # 删除该目录及其所有内容

shutil.disk_usage(path)

shutil.disk_usage(path: Union[str, bytes, PathLike]) -> shutil._ntuple_diskusage
  • 作用:返回磁盘使用情况,命名元组包含 totalusedfree(字节数)。
  • 示例
usage = shutil.disk_usage("/")print(f"总空间: {usage.total // (1024**3)} GB")

3.3 归档与解压

shutil.make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)

shutil.make_archive(base_name: str, format: str, root_dir: Optional[str] = None, base_dir: Optional[str] = None, ...) -> str
  • 作用:创建压缩归档文件(如 zip、tar)。
  • 参数
    • base_name – 输出文件的前缀(不含扩展名)。
    • format – 支持 'zip''tar''gztar''bztar''xztar'
    • root_dir – 归档的根目录(所有路径相对于此)。
    • base_dir – 要归档的目录(默认为当前目录)。
  • 返回值:生成的归档文件完整路径。
  • 示例
shutil.make_archive("backup""zip", root_dir="/home/user/data")

shutil.unpack_archive(filename, extract_dir=None, format=None)

shutil.unpack_archive(filename: Union[str, bytes, PathLike], extract_dir: Optional[Union[str, bytes, PathLike]] = None, format: Optional[str] = None) -> None
  • 作用:解压归档文件。
  • 参数
    • extract_dir – 解压目标目录,默认当前目录。
    • format – 自动检测(无需指定),可强制指定。
  • 示例
shutil.unpack_archive("backup.zip""output/")

四、pathlib 模块 – 面向对象的路径

4.1 创建 Path 对象

from pathlib import Path# 当前目录p = Path()# 绝对路径p = Path("/home/user/file.txt")# 相对路径p = Path("data/input.csv")# 组合(推荐)p = Path.home() / "documents" / "notes.txt"

4.2 路径组件访问

属性/方法
返回值示例
说明
p.name'file.txt'
最后一部分(文件名或目录名)
p.stem'file'
无扩展名的文件名
p.suffix'.txt'
扩展名(含点)
p.suffixes['.tar', '.gz']
所有扩展名列表
p.parentPath('/home/user')
父目录
p.parents[Path('/home/user'), Path('/home'), Path('/')]
所有祖先目录(可索引)
p.anchor'/'
 (Linux) 或 'C:\\' (Windows)
根路径部分
p.drive'C:'
 (Windows)
驱动器符
p.root'\\'
 (Windows) 或 '/' (Linux)
根路径

4.3 目录操作

Path.mkdir(mode=0o777, parents=False, exist_ok=False)

defmkdir(self, mode: int = 0o777, parents: bool = False, exist_ok: bool = False) -> None
  • 作用:创建目录。
  • 参数
    • parents=True – 自动创建不存在的父目录(类似 os.makedirs)。
    • exist_ok=True – 目录已存在时不报错。
  • 示例
Path("a/b/c").mkdir(parents=True, exist_ok=True)

Path.rmdir()

defrmdir(self) -> None
  • 作用:删除空目录。非空时抛出 OSError

Path.iterdir()

defiterdir(self) -> Iterator[Path]
  • 作用:生成当前目录下的所有条目(文件和子目录)的 Path 对象迭代器。
  • 示例
for child in Path(".").iterdir():if child.is_file():        print(f"文件: {child.name}")

Path.walk(top_down=True, on_error=None, follow_symlinks=False)

defwalk(self, top_down: bool = True, on_error: Optional[Callable] = None, follow_symlinks: bool = False) -> Iterator[Tuple[Path, List[str], List[str]]]
  • 作用:Python 3.12+ 引入,遍历目录树,生成 (root, dirs, files) 三元组(dirs 和 files 是字符串列表)。
  • 示例
for root, dirs, files in Path("project").walk():for file in files:        print(root / file)

4.4 模式匹配

Path.glob(pattern)

defglob(self, pattern: str) -> Iterator[Path]
  • 作用:非递归匹配当前目录下的文件/目录(** 无效),返回生成器。
  • 示例
for py in Path("src").glob("*.py"):    print(py)

Path.rglob(pattern)

defrglob(self, pattern: str) -> Iterator[Path]
  • 作用:递归匹配,相当于 glob("**/" + pattern)
  • 示例
# 查找所有子目录中的 .txt 文件txt_files = list(Path(".").rglob("*.txt"))

4.5 文件操作

Path.read_text(encoding=None, errors=None)

defread_text(self, encoding: Optional[str] = None, errors: Optional[str] = None) -> str
  • 作用:以文本模式读取文件全部内容,返回字符串。
  • 注意:文件不存在时抛出 FileNotFoundError
  • 示例
content = Path("config.json").read_text(encoding="utf-8")

Path.write_text(data, encoding=None, errors=None)

defwrite_text(self, data: str, encoding: Optional[str] = None, errors: Optional[str] = None) -> int
  • 作用:将字符串写入文件(覆盖模式)。返回写入的字符数。
  • 示例
Path("output.txt").write_text("Hello, World!", encoding="utf-8")

Path.read_bytes() / Path.write_bytes(data)

defread_bytes(self) -> bytesdef write_bytes(self, data: bytes) -> int
  • 作用:二进制读写。

Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)

defopen(self, mode: str = 'r', buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None) -> IO
  • 作用:打开文件,返回文件对象。与内置 open() 参数相同,但更简洁。
  • 示例
with Path("data.bin").open("rb"as f:    data = f.read()

Path.unlink(missing_ok=False)

defunlink(self, missing_ok: bool = False) -> None
  • 作用:删除文件。
  • 参数missing_ok=True – 文件不存在时不抛出 FileNotFoundError
  • 示例
Path("temp.txt").unlink(missing_ok=True)

4.6 属性与判断

方法
说明
p.exists()
路径是否存在
p.is_file()
是否为普通文件
p.is_dir()
是否为目录
p.is_symlink()
是否为符号链接
p.is_absolute()
是否为绝对路径
p.stat()
返回 os.stat_result,用法同 os.stat
p.resolve()
解析符号链接并返回绝对路径(规范化的)
p.absolute()
返回绝对路径(不解析符号链接)

4.7 路径拼接与转换

# 使用 / 运算符home = Path.home()config = home / ".config" / "app" / "settings.ini"# 转换为字符串str_path = str(config)    # '/home/user/.config/app/settings.ini'# 转换为 os 风格路径(用于旧 API)os_path = config.as_posix()   # 总是使用 '/' 分隔符

五、总结对比表(按函数功能索引)

需求
os / os.path
pathlib
shutil
glob
获取当前目录
os.getcwd()Path.cwd()
-
-
改变目录
os.chdir()
-
-
-
列出目录
os.listdir()Path.iterdir()
-
-
创建单级目录
os.mkdir()Path.mkdir()
-
-
创建多级目录
os.makedirs()Path.mkdir(parents=True)
-
-
删除空目录
os.rmdir()Path.rmdir()
-
-
删除非空目录
-
-
shutil.rmtree()
-
删除文件
os.remove()Path.unlink()
-
-
重命名/移动
os.rename()Path.rename()
 / Path.replace()
shutil.move()
-
复制文件
-
-
shutil.copy2()
-
复制目录
-
-
shutil.copytree()
-
路径拼接
os.path.join()/
 运算符
-
-
文件/目录是否存在
os.path.exists()Path.exists()
-
-
获取文件大小
os.path.getsize()Path.stat().st_size
-
-
遍历目录树
os.walk()Path.walk()
 (3.12+) / Path.rglob()
-
-
按模式搜索
glob.glob()
 / glob.iglob()
Path.glob()
 / Path.rglob()
-
glob.glob()
读取文本文件
open()
 + read
Path.read_text()
-
-
写入文本文件
open()
 + write
Path.write_text()
-
-
创建压缩包
-
-
shutil.make_archive()
-
解压压缩包
-
-
shutil.unpack_archive()
-
环境变量
os.environ
-
-
-
执行系统命令
os.system()
 (不推荐)
-
-
-

六、常见陷阱与最佳实践

  1. 路径拼接不要用字符串加法❌ path = root + "/" + filename(Windows 下错误)✅ os.path.join(root, filename) 或 Path(root) / filename

  2. 删除非空目录不要用 os.rmdir❌ os.rmdir("non_empty") → OSError✅ shutil.rmtree("non_empty")

  3. 跨设备移动文件不要用 os.rename❌ 跨分区移动会失败✅ shutil.move(src, dst)

  4. 遍历目录时修改正在遍历的列表os.walk 中修改 dirnames 可以控制子目录遍历(修剪),但不要直接修改 filenames 列表来删除文件(会导致跳过条目)。删除文件应在遍历到该文件时执行。

  5. glob 不会自动隐藏文件模式 *.txt 不会匹配 .hidden.txt,需显式写 .*.txt 或 .??*

  6. pathlib 的 / 运算符性能虽然优雅,但频繁大量拼接时,os.path.join 稍快(微秒级差异)。日常使用无感知。

  7. 注意 shutil.copy 与 shutil.copy2 的区别备份重要文件时使用 copy2 保留元数据;临时复制用 copy 更快。

  8. 安全写入文件(防止写一半崩溃)推荐使用临时文件 + 原子替换:

from pathlib import Pathimport tempfiledefatomic_write(path: Path, content: str):with tempfile.NamedTemporaryFile(dir=path.parent, delete=Falseas tf:        tf.write(content.encode())        tmp_path = Path(tf.name)   tmp_path.replace(path)  # 原子操作(Unix)

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-07-03 19:17:48 HTTP/2.0 GET : https://f.mffb.com.cn/a/486906.html
  2. 运行时间 : 0.176821s [ 吞吐率:5.66req/s ] 内存消耗:4,677.26kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=4c0cce9aa7a452d7683154d3c06cea7d
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000384s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000798s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000317s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000282s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000482s ]
  6. SELECT * FROM `set` [ RunTime:0.000192s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000557s ]
  8. SELECT * FROM `article` WHERE `id` = 486906 LIMIT 1 [ RunTime:0.002011s ]
  9. UPDATE `article` SET `lasttime` = 1783077468 WHERE `id` = 486906 [ RunTime:0.023815s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000283s ]
  11. SELECT * FROM `article` WHERE `id` < 486906 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.001287s ]
  12. SELECT * FROM `article` WHERE `id` > 486906 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.000424s ]
  13. SELECT * FROM `article` WHERE `id` < 486906 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.012805s ]
  14. SELECT * FROM `article` WHERE `id` < 486906 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.041478s ]
  15. SELECT * FROM `article` WHERE `id` < 486906 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.023778s ]
0.179282s