QQ个性网:专注于分享免费的QQ个性内容

关于我们| 网站公告| 广告服务| 联系我们| 网站地图

搜索
编程 JavaScript Java C++ Python SQL C Io ML COBOL Racket APL OCaml ABC Sed Bash Visual Basic Modula-2 Logo Delphi IDL Groovy Julia REXX Chapel X10 Forth Eiffel C# Go Rust PHP Swift Kotlin R Dart Perl Ruby TypeScript MATLAB Shell Lua Scala Objective-C F# Haskell Elixir Lisp Prolog Ada Fortran Erlang Scheme Smalltalk ABAP D ActionScript Tcl AWK IDL J PostScript IDL PL/SQL PowerShell

Python之pathlib库的使用方法

日期:2025/04/03 21:36来源:未知 人气:53

导读:Pathlib 是一个用于处理文件路径的 Python 库,提供了许多实用的函数和方法来处理文件系统路径。它提供了一个面向对象的层次结构,路径被表示为对象,这些对象拥有一些属性和方法,使得文件路径的操作变得更加直观和方便。1、获取当前目录# -*- coding:utf-8 -*-from pathlib import Pathprint(Path.cwd()) # 返回当前目......

Pathlib 是一个用于处理文件路径的 Python 库,提供了许多实用的函数和方法来处理文件系统路径。它提供了一个面向对象的层次结构,路径被表示为对象,这些对象拥有一些属性和方法,使得文件路径的操作变得更加直观和方便。

1、获取当前目录

-- coding:utf-8 --

from pathlib import Path

print(Path.cwd()) # 返回当前目录 print(Path.home()) # 输出用户家目录

运行结果: C:\Users\caiya\Desktop\work\demo C:\Users\caiya

2、获取上级目录

-- coding:utf-8 --

from pathlib import Path

filename = r"C:\Users\caiya\Desktop\work\demo\temp\123.txt" res = Path(filename)

print(res.parent) # 返回上级目录 print(res.parents) # 返回上级目录列表(可进行迭代)

运行结果: C:\Users\caiya\Desktop\work\demo

3、文件拆分

-- coding:utf-8 --

from pathlib import Path

filename = r"C:\Users\caiya\Desktop\work\demo\temp\123.txt" res = Path(filename)

print(res.name) # 获取文件名 print(res.stem) # 获取文件名前缀 print(res.suffix) # 获取文件名后缀

运行结果: 123.txt 123 .txt

4、判断文件是否存在

-- coding:utf-8 --

from pathlib import Path

name = r"demo.txt" res = Path(name)

判断对象是否存在,对象:文件或目录

if res.exists(): print("存在") else: print("不存在")

5、判断文件是否存在,不存在则创建

-- coding:utf-8 --

from pathlib import Path

name = r"demo.txt" res = Path(name)

判断对象是否存在,对象:文件或目录

ifnot res.exists(): withopen(name, "w") as f: f.write("Hello World") print("文件不存在,已创建成功")

6、判断目录是否存在,不存在则创建

-- coding:utf-8 --

from pathlib import Path

name = r"test" res = Path(name)

判断对象是否存在,对象:文件或目录

ifnot res.exists(): res.mkdir() print("目录不存在,已经创建完成")

7、判断目录是否存在,不存则创建(递归创建)

-- coding:utf-8 --

from pathlib import Path

name = r"111\222\333" res = Path(name)

判断对象是否存在,对象:文件或目录

ifnot res.exists(): res.mkdir(parents=True) print("目录不存在,已经创建完成")

8、获取文件属性

-- coding:utf-8 --

from pathlib import Path import time

filename = r"demo.txt" res = Path(filename)

格式化时间戳

defformat_time(s): local_time = time.localtime(s) format_time = "%Y-%m-%d %H:%M:%S" now_time = time.strftime(format_time, local_time) return now_time

t = res.stat() print(f"文件大小:{t.st_size}") print(f"文件创建时间:{format_time(t.st_ctime)}") print(f"文件修改时间:{format_time(t.st_mtime)}") print(f"文件访问时间:{format_time(t.st_atime)}")

运行结果 文件大小:11 文件创建时间:2023-01-1714:01:41 文件修改时间:2023-01-1714:01:41 文件访问时间:2023-01-1714:01:41

9、文件重命名

-- coding:utf-8 --

from pathlib import Path

filename = r"demo.txt" res = Path(filename)

文件重命名

res.rename(r"demo_test.txt")

10、删除文件

-- coding:utf-8 --

from pathlib import Path

filename = r"demo.txt" res = Path(filename)

删除文件

if res.is_file(): res.unlink() print("删除成功") else: print("文件不存")

11、删除文件夹

-- coding:utf-8 --

from pathlib import Path

filename = r"test" res = Path(filename)

if res.is_dir(): res.rmdir() print("删除成功") else: print("文件夹不存在")

12、删除非空文件夹

-- coding:utf-8 --

from pathlib import Path import shutil

filename = r"111" res = Path(filename)

if res.is_dir(): shutil.rmtree(filename) print("删除成功") else: print("文件夹不存在")

13、遍历文件和文件夹

-- coding:utf-8 --

from pathlib import Path

filename = r"dist" res = Path(filename)

for item in res.rglob("*"): print(item)

运行结果:

dist\系统小工具助手 dist\系统小工具助手\base_library.zip dist\系统小工具助手\d3dcompiler_47.dll dist\系统小工具助手\images dist\系统小工具助手\libcrypto-1_1-x64.dll dist\系统小工具助手\libcrypto-1_1.dll dist\系统小工具助手\libeay32.dll dist\系统小工具助手\libEGL.dll

14、遍历文件夹

-- coding:utf-8 --

from pathlib import Path

filename = r"dist" res = Path(filename)

for item in res.rglob("**"): print(item)

运行结果

dist dist\系统小工具助手 dist\系统小工具助手\images dist\系统小工具助手\PyQt5 dist\系统小工具助手\PyQt5\Qt5 dist\系统小工具助手\PyQt5\Qt5\plugins dist\系统小工具助手\PyQt5\Qt5\plugins\audio dist\系统小工具助手\PyQt5\Qt5\plugins\bearer dist\系统小工具助手\PyQt5\Qt5\plugins\geoservices

15、遍历文件

-- coding:utf-8 --

from pathlib import Path

filename = r"dist" res = Path(filename)

for item in res.rglob("*"): temp_file = Path(item) if temp_file.is_file(): print(item)

运行结果

dist\系统小工具助手\base_library.zip dist\系统小工具助手\d3dcompiler_47.dll dist\系统小工具助手\libcrypto-1_1-x64.dll dist\系统小工具助手\libcrypto-1_1.dll dist\系统小工具助手\libeay32.dll dist\系统小工具助手\libEGL.dll dist\系统小工具助手\libGLESv2.dll dist\系统小工具助手\libssl-1_1-x64.dll dist\系统小工具助手\MSVCP140.dll dist\系统小工具助手\MSVCP140_1.dll

深度好文计划

关于我们|网站公告|广告服务|联系我们| 网站地图

Copyright © 2002-2023 某某QQ个性网 版权所有 | 备案号:粤ICP备xxxxxxxx号

声明: 本站非腾讯QQ官方网站 所有软件和文章来自互联网 如有异议 请与本站联系 本站为非赢利性网站 不接受任何赞助和广告