参考链接: os.path.basename(path)
参考链接: os.path.dirname(path)
代码实验展示:
import os
# --------------------------------------------------------#
filePath = r"D:\Python\Python35\Lib\site-packages\jieba\analyse\idf.txt"
print('路径为:',filePath)
print(os.path.basename(filePath))
print(os.path.dirname(filePath))
# --------------------------------------------------------#
filePath = r'D:\Python\Python35\Lib\site-packages\jieba'
print('路径为:',filePath)
print(os.path.basename(filePath))
print(os.path.dirname(filePath))
控制台结果输出展示:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 1017 毫秒。
(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' '52785' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\my-experiments\test2.py'
路径为: D:\Python\Python35\Lib\site-packages\jieba\analyse\idf.txt
idf.txt
D:\Python\Python35\Lib\site-packages\jieba\analyse
路径为: D:\Python\Python35\Lib\site-packages\jieba
jieba
D:\Python\Python35\Lib\site-packages
(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>