pathlib使用学习笔记

MirrorYuChen
MirrorYuChen
发布于 2025-01-13 / 10 阅读
0
0

pathlib使用学习笔记

pathlib使用学习笔记

​ pathlibPython一个模块,用于处理文件系统路径。它提供了一个面向对象的方式来操作路径,使路径操作更加直观和方便。笔记主要内容如下:

1.安装

>> pip install pathlib

2.路径属性

属性 描述
.name 文件名(包括后缀),如果是目录则获取目录名
.stem 文件名,不包含后缀
.suffix 后缀,如,.txt.jpg,...
.parent 父级目录,相当于 cd ..
.anchor 最上层目录,如 c:\/

​ 举例说明:

>> python
Python 3.8.10 (default, Nov  7 2024, 13:10:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> cwd = Path.cwd()
>>> cwd
PosixPath('/home/mirror/workspace')
>>> file = cwd / 'test.jpg'
>>> file
PosixPath('/home/mirror/workspace/test.jpg')
>>> print(file.name)
test.jpg
>>> print(file.stem)
test
>>> print(file.suffix)
.jpg
>>> print(file.parent)
/home/mirror/workspace
>>> print(file.anchor)
/

3.路径操作

  • (1) 路径判断:is_file()判断是否为文件,is_dir()判断是否为目录
>> python
Python 3.8.10 (default, Nov  7 2024, 13:10:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> cwd = Path.cwd()
>>> cwd.is_dir()
True
>>> file = cwd / 'main.py'
>>> file.is_file()
True
  • (2) 路径操作:mkdir()用于创建路径,rmdir()用于删除路径:
>> python
Python 3.8.10 (default, Nov  7 2024, 13:10:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> cwd = Path.cwd()
>>> p = cwd / 'test'
>>> p.is_dir()
False
>>> p.mkdir(parents=True)
>>> p.is_dir()
True
>>> p.rmdir()
>>> p.is_dir()
False
  • (3) 路径拼接:Path支持 /拼接路径:
>> python
Python 3.8.10 (default, Nov  7 2024, 13:10:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> cwd = Path.cwd()
>>> p = cwd / 'dist'
>>> p.is_dir()
True

4.文件操作

  • (1) 文件扫描:glob()只扫描当前路径,rglob()扫描当前路径及所有子路径
>> Python 3.8.10 (default, Nov  7 2024, 13:10:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> cwd = Path.cwd()
>>> files = [str(file) for file in cwd.glob('*.py')]
>>> print(files)
['/home/mirror/workspace/python/main.py', '/home/mirror/workspace/python/train.py']
>>> files = [str(file) for file in cwd.rglob('*.py')]
>>> print(files)
['/home/mirror/workspace/python/main.py', '/home/mirror/workspace/python/train.py', '/home/mirror/workspace/python/dist/train.py', '/home/mirror/workspace/python/dist/pyarmor_runtime_000000/__init__.py']
  • (2) 文件操作:touch()用于创建文件,unlink()用于删除文件
>> python
Python 3.8.10 (default, Nov  7 2024, 13:10:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> cwd = Path.cwd()
>>> file = cwd / 'test.txt'
>>> file.is_file()
False
>>> file.touch(exist_ok=True)
>>> file.is_file()
True
>>> file.unlink()
>>> file.is_file()
False

5.软链接

  • symlin_to用于创建指向目标路径的符号链接,也即是说 src_path是存在的路径,dst_path是其对应的软链接
>> ls
dist  main.py  train.py
>> mkdir link
>> python
Python 3.8.10 (default, Nov  7 2024, 13:10:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>> from pathlib import Path
>> src_path = Path.cwd() / 'main.py'
>> dst_path = Path.cwd() / 'link/link.py'
>> dst_path.symlink_to(src_path)

6.一些工程技巧

  • (1) python脚本中启动另外一个同级目录的 python脚本:
'''
Author: chenjingyu
Date: 2024-12-24 15:38:23
Contact: 2458006466@qq.com
Description: main
'''
import os
from pathlib import Path

curr_path = Path(__file__).parent

if __name__ == '__main__':
  cmd = f'python {curr_path}/test.py'
  os.system(cmd)
'''
Author: chenjingyu
Date: 2025-01-13 16:11:06
Contact: 2458006466@qq.com
Description: test
'''
if __name__ == '__main__':
  print("Hello, World!")
  • (2) 将当前路径添加到系统环境变量中,以便包导入:
'''
Author: chenjingyu
Date: 2024-12-24 15:38:23
Contact: 2458006466@qq.com
Description: main
'''
import sys
from pathlib import Path

curr_path = Path(__file__).parent
if str(curr_path) not in sys.path:
  sys.path.append(str(curr_path))

7.参考资料


评论