从python中的路径获取子文件名,而不使用拆分

2024-10-02 18:18:16 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个类似C:\\exportDir\\testing\\content\\catalog\\root\\shared\\First\\First_content\\Report1的路径

在我的案例Report1中,如何检索子文件名,而不使用按\\拆分


Tags: 路径文件名rootcontenttesting案例firstshared
1条回答
网友
1楼 · 发布于 2024-10-02 18:18:16

使用os.path.split()

例如:

import os 
  
# path 
path = '/home/User/Desktop/file.txt'
  
# Split the path in  
# head and tail pair 
head_tail = os.path.split(path) 
  
# print head and tail 
# of the specified path 
print("Head of '% s:'" % path, head_tail[0]) 
print("Tail of '% s:'" % path, head_tail[1], "\n") 

相关问题 更多 >