• Python内置库pathlib


    获取文件名

    from pathlib import Path  # 导入pathlib的Path类
    path = "/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb"
    p = Path(path)
    print(f"获取文件名:{p.name}")  # 获取文件名:pathlib库的使用.ipynb
    
    • 1
    • 2
    • 3
    • 4

    获取文件前缀和后缀

    from pathlib import Path
    path = "/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb"
    p = Path(path)
    print(f"获取前缀:{p.stem}")  # 获取前缀:pathlib库的使用
    print(f"获取后缀:{p.suffix}")  # 获取后缀:.ipynb
    
    • 1
    • 2
    • 3
    • 4
    • 5

    获取文件的文件夹及上一级、上上级文件夹

    from pathlib import Path
    path = "/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb"
    p = Path(path)
    print(f"获取当前文件所属文件夹:{p.parent}") 
    print(f"获取上一级文件夹:{p.parent.parent}")
    print(f"获取上上一级文件夹:{p.parent.parent.parent}")
    """
    获取当前文件所属文件夹:/home/leovin/JupyterNotebookFolders
    获取上一级文件夹:/home/leovin
    获取上上一级文件夹:/home
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    获取该文件所属的文件夹及其父文件夹

    from pathlib import Path
    path = "/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb"
    p = Path(path)
    print(f"获取当前文件所属文件夹及其父文件夹:{p.parents}\n")
    # 遍历
    for idx, folder_path in enumerate(p.parents):
        print(f"No.{idx}: {folder_path}")
    """
    获取当前文件所属文件夹及其父文件夹:
    
    No.0: /home/leovin/JupyterNotebookFolders
    No.1: /home/leovin
    No.2: /home
    No.3: /
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    文件绝对路径

    from pathlib import Path
    import os
    path = "/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb"
    p = Path(path)
    print(f"将文件的绝对路径按照`/`进行分割,返回一个tuple:{p.parts}\n")
    # 遍历
    for idx, element in enumerate(p.parts):
        print(f"No.{idx}: {element}")
    """
    获取当前文件所属文件夹及其父文件夹:
    No.0: /
    No.1: home
    No.2: leovin
    No.3: JupyterNotebookFolders
    No.4: pathlib库的使用.ipynb
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    获取当前工作目录

    from pathlib import Path
    path = Path.cwd()
    print(f"Path.cwd(): {path}") # Path.cwd(): /home/leovin/JupyterNotebookFolders
    
    • 1
    • 2
    • 3

    获取用户

    from pathlib import Path
    print(f"获取用户home路径: {Path.home()}") # 获取用户home路径: /home/leovin
    
    • 1
    • 2

    获取文件详细信息

    from pathlib import Path
    p = Path("/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb")
    print(f"获取文件详细信息: {p.stat()}\n")
    print(f"获取文件字节大小: {p.stat().st_size}\n")
    print(f"获取文件创建时间: {p.stat().st_ctime}\n")  # c: create
    print(f"获取文件上次修改时间: {p.stat().st_mtime}\n")  # m:: modify
    """
    获取文件详细信息: os.stat_result(st_mode=33204, st_ino=23860358, st_dev=2052, st_nlink=1, st_uid=1000, st_gid=1000, st_size=12125, st_atime=1653039478, st_mtime=1653039478, st_ctime=1653039478)
    
    获取文件字节大小: 12125
    
    获取文件创建时间: 1653039478.703161
    
    获取文件上次修改时间: 1653039478.703161
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    检查目录或者文件是否存在

    from pathlib import Path
    print(f"目标路径的文件是否存在: {Path('/home/leovin/JupyterNotebookFolders/xxx').exists()}")  # False
    print(f"目标路径的文件是否存在: {Path('/home/leovin/JupyterNotebookFolders').exists()}")  # True
    
    • 1
    • 2
    • 3

    遍历一个目录

    from pathlib import Path
    path_object = Path("/home/leovin/JupyterNotebookFolders/").iterdir()
    print(f"path_object: {path_object}")  # path_object: 
    # 迭代目录对象
    for idx, element in enumerate(path_object):
        print(f"No.{idx}: {element}")
    """
    path_object: 
    No.0: /home/leovin/JupyterNotebookFolders/temp
    No.1: /home/leovin/JupyterNotebookFolders/torch.meshgrid().ipynb
    No.2: /home/leovin/JupyterNotebookFolders/array.argsort().ipynb
    No.3: /home/leovin/JupyterNotebookFolders/切片....ipynb
    No.4: /home/leovin/JupyterNotebookFolders/logging信息.log
    No.5: /home/leovin/JupyterNotebookFolders/Python中类的私有变量、私有方法、静态方法.ipynb
    No.6: /home/leovin/JupyterNotebookFolders/temp_file.ipynb
    No.7: /home/leovin/JupyterNotebookFolders/example.log
    No.8: /home/leovin/JupyterNotebookFolders/Python语法.ipynb
    No.9: /home/leovin/JupyterNotebookFolders/.ipynb_checkpoints
    No.10: /home/leovin/JupyterNotebookFolders/craw.data.txt
    No.11: /home/leovin/JupyterNotebookFolders/test.txt
    No.12: /home/leovin/JupyterNotebookFolders/logging的学习.ipynb
    No.13: /home/leovin/JupyterNotebookFolders/Test.ipynb
    No.14: /home/leovin/JupyterNotebookFolders/算法题
    No.15: /home/leovin/JupyterNotebookFolders/functions.ipynb
    No.16: /home/leovin/JupyterNotebookFolders/mask转0&1.ipynb
    No.17: /home/leovin/JupyterNotebookFolders/Multi_Task_in_Python.ipynb
    No.18: /home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb
    No.19: /home/leovin/JupyterNotebookFolders/三种激活函数绘制.ipynb
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    获取所有符合pattern的文件

    from pathlib import Path
    pattern = "JupyterNotebookFolders/*.ipynb"
    glob_generator = Path("/home/leovin/").glob(pattern)
    # 遍历返回的对象 -> 返回的是绝对路径
    for idx, element in enumerate(glob_generator):
        print(f"No.{idx}: {element}")
    """
    No.0: /home/leovin/JupyterNotebookFolders/torch.meshgrid().ipynb
    No.1: /home/leovin/JupyterNotebookFolders/array.argsort().ipynb
    No.2: /home/leovin/JupyterNotebookFolders/切片....ipynb
    No.3: /home/leovin/JupyterNotebookFolders/Python中类的私有变量、私有方法、静态方法.ipynb
    No.4: /home/leovin/JupyterNotebookFolders/temp_file.ipynb
    No.5: /home/leovin/JupyterNotebookFolders/Python语法.ipynb
    No.6: /home/leovin/JupyterNotebookFolders/logging的学习.ipynb
    No.7: /home/leovin/JupyterNotebookFolders/Test.ipynb
    No.8: /home/leovin/JupyterNotebookFolders/functions.ipynb
    No.9: /home/leovin/JupyterNotebookFolders/mask转0&1.ipynb
    No.10: /home/leovin/JupyterNotebookFolders/Multi_Task_in_Python.ipynb
    No.11: /home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb
    No.12: /home/leovin/JupyterNotebookFolders/三种激活函数绘制.ipynb
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    pathlib库与os库的对比

    在这里插入图片描述
    参考:https://zhuanlan.zhihu.com/p/475661402

  • 相关阅读:
    大数据Flink(一百):SQL自定义函数(UDF)和标量函数(Scalar Function)
    linux日志查看技巧
    JDBC(二)
    低调而无为而治,藏在超级应用背后的道家哲学
    CCF CSP题解:数字排序(201503-2)
    C#生成自定义海报
    产品新闻稿撰写流程是怎样的,纯干货
    (十)延迟队列
    arthas线上问题检查思路
    Docker基于Minio搭建对象(文件)存储服务
  • 原文地址:https://blog.csdn.net/taifyang/article/details/127715832