python使用os库的os.path.exists、os.path.isdir、os.path.isfile、os.path.isabs来判断指定路径是否存在、是否为目录、是否为文件、是否为绝对路径

参考链接: os.path.exists(path)
参考链接: os.path.isfile(path)
参考链接: os.path.isdir(path)
参考链接: os.path.isabs(path)

在这里插入图片描述在这里插入图片描述在这里插入图片描述

实验1:

import os
folderPathExists = r'D:\Python\Python35\Lib\site-packages\jieba'
folderPathNotExists = r'D:\曹雪芹Python\曹雪芹Python35\曹雪芹Lib\曹雪芹site-packages\jieba'
print(os.path.exists(folderPathExists))
print(os.path.exists(folderPathNotExists))

控制台下结果输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 1087 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>  & 'D:\Anaconda3\envs\pytorch_1.7.1_cu102\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '51195' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\my-experiments\test.py'
True
False
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>

实验2:

import os
folderPath = r'D:\Python\Python35\Lib\site-packages\jieba'
filePath = r"D:\Python\Python35\Lib\site-packages\jieba\__main__.py"
print(os.path.isfile(folderPath))
print(os.path.isfile(filePath))

控制台下结果输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 854 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>  & 'D:\Anaconda3\envs\pytorch_1.7.1_cu102\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '51283' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\my-experiments\test.py'
False
True
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>

实验3:

import os
folderPath = r'D:\Python\Python35\Lib\site-packages\jieba'
filePath = r"D:\Python\Python35\Lib\site-packages\jieba\__main__.py"
print(os.path.isdir(folderPath))
print(os.path.isdir(filePath))

控制台下结果输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 835 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>  & 'D:\Anaconda3\envs\pytorch_1.7.1_cu102\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '51296' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\my-experiments\test.py'
True
False
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>

实验4:

import os
# --------------------------------------------------------#
filePath = r"D:\Python\Python35\Lib\site-packages\jieba\analyse\idf.txt"
print('路径为:',filePath)
print(os.path.isabs(filePath))

# --------------------------------------------------------#
filePath = r'我的文件.txt'
print('路径为:',filePath)
print(os.path.isabs(filePath))

控制台下结果输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 999 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>  & 'D:\Anaconda3\envs\pytorch_1.7.1_cu102\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '52919' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\my-experiments\test2.py'
路径为: D:\Python\Python35\Lib\site-packages\jieba\analyse\idf.txt
True
路径为: 我的文件.txt
False
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> 
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值