python os.path

import os
p=os.path.dirname('C:\\win\\yes\\text.txt')
print p
print os.path.split(p+"\\")#目录与文件分离
print os.path.split(p)

print os.path.dirname(p)#the first half of the pair returned by split(path).
print os.path.basename(p)#the second half of the pair returned by split(path).

print os.path.normcase('C:\\win\\yes\\..\\..')#将正斜杠换位反斜杠
print os.path.normcase('/usr/var/../')

print os.path.normpath('/usr/var/../yes.c')#序列化路径,去除上一个目录(../)与当前目录(./)
print os.path.normpath('C:\\win\\yes\\..\\..')

 输出:

C:\win\yes
('C:\\win\\yes', '')
('C:\\win', 'yes')
C:\win
yes
c:\win\yes\..\..
\usr\var\..\
\usr\yes.c
C:\

原文地址:https://www.cnblogs.com/UnGeek/p/2847157.html