from pathlib import Path # 导入pathlib的Path类
path = "/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb"
p = Path(path)
print(f"获取文件名:{p.name}") # 获取文件名:pathlib库的使用.ipynb
from pathlib import Path
path = "/home/leovin/JupyterNotebookFolders/pathlib库的使用.ipynb"
p = Path(path)
print(f"获取前缀:{p.stem}") # 获取前缀:pathlib库的使用
print(f"获取后缀:{p.suffix}") # 获取后缀:.ipynb
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
"""
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: /
"""
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
"""
from pathlib import Path
path = Path.cwd()
print(f"Path.cwd(): {path}") # Path.cwd(): /home/leovin/JupyterNotebookFolders
from pathlib import Path
print(f"获取用户home路径: {Path.home()}") # 获取用户home路径: /home/leovin
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
"""
from pathlib import Path
print(f"目标路径的文件是否存在: {Path('/home/leovin/JupyterNotebookFolders/xxx').exists()}") # False
print(f"目标路径的文件是否存在: {Path('/home/leovin/JupyterNotebookFolders').exists()}") # True
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
"""
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
"""
参考:https://zhuanlan.zhihu.com/p/475661402