Python 技术篇-不使用os模块判断指定路径是文件还是文件夹,使用pathlib库判断文件和文件夹

pathlib.Path("路径").is_file() 判断是否是文件,是文件的话返回 True
pathlib.Path("路径").is_dir() 判断是否是文件夹,是文件夹的话返回 True

# -*- coding: UTF8 -*-
import pathlib

path = pathlib.Path("C:\\Users\\Administrator\\Desktop\\办公\\0-桌面\\spr合集")
print("路径C:\\Users\\Administrator\\Desktop\\办公\\0-桌面\\spr合集")
print("是否是文件:" + str(path.is_file()))
print("是否是文件夹:" + str(path.is_dir()))

path = pathlib.Path("C:\\Users\\Administrator\\Desktop\\办公\\0-桌面\\spr合集\\达梦8-SPR-首页-查询模块下应用.html")
print("路径C:\\Users\\Administrator\\Desktop\\办公\\0-桌面\\spr合集\\达梦8-SPR-首页-查询模块下应用.html")
print("是否是文件:" + str(path.is_file()))
print("是否是文件夹:" + str(path.is_dir()))

在这里插入图片描述
运行效果图:

在这里插入图片描述
喜欢的点个赞❤吧!

猜你喜欢

转载自blog.csdn.net/qq_38161040/article/details/109050022