pathlib简单使用, 比os简单

from pathlib import Path

p = Path(r'D:projectpachong	est1a.txt')

# 基本用法
'''
# 判断
p.is_dir()   # 判断是否是目录
p.is_file()  # 判断是否是文件
p.exists()   # 判断路径是否存在
p.is_absolute()  # 判断是否是绝对路径



# 路径
p.resolve()  # 返回绝对路径
/            # 拼接路径
p.joinpath() # 拼接路径
p.cwd()      # 返回当前路径


# 文件操作
p.unlink()       # 删除文件或目录
p.chmod()        # 修改文件权限
p.rename()       # 重命名
p.with_name()    # 修改文件名,非真实修改
p.with_suffix()  # 修改后缀名,非真实修改



# 基本属性
p.parts      # 分割路径,返回元祖

p.drive      # 返回盘符
p.root       # 返回根目录
p.anchor     # 自动判断是drive还是root

p.parent     # 返回上级目录
p.parents    # 返回所有上级目录

p.name       # 返回文件名,带后缀
p.stem       # 返回文件名,不带后缀
p.suffix     # 返回后缀
p.suffixes   # 返回所有后缀,列表

p.stat()     # 返回文件属性
p.glob()     # 匹配查找文件
p.iterdir()  # 遍历目录中的子目录或者文件

'''

os 和 os.path

pathlib

os.path.abspath()

Path.resolve()

os.chmod()

Path.chmod()

os.mkdir()

Path.mkdir()

os.makedirs()

Path.mkdir()

os.rename()

Path.rename()

os.replace()

Path.replace()

os.rmdir()

Path.rmdir()

os.remove()os.unlink()

Path.unlink()

os.getcwd()

Path.cwd()

os.path.exists()

Path.exists()

os.path.expanduser()

Path.expanduser() 和 Path.home()

os.listdir()

Path.iterdir()

os.path.isdir()

Path.is_dir()

os.path.isfile()

Path.is_file()

os.path.islink()

Path.is_symlink()

os.link()

Path.link_to()

os.symlink()

Path.symlink_to()

os.readlink()

Path.readlink()

os.stat()

Path.stat()Path.owner()Path.group()

os.path.isabs()

PurePath.is_absolute()

os.path.join()

PurePath.joinpath()

os.path.basename()

PurePath.name

os.path.dirname()

PurePath.parent

os.path.samefile()

Path.samefile()

os.path.splitext()

PurePath.suffix

原文地址:https://www.cnblogs.com/shiyixirui/p/14347757.html