os.path.splitext()
os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
代码示例
import os
path_01='D:/User/wgy/workplace/data/notMNIST_large.tar.gar'
path_02='D:/User/wgy/workplace/data/notMNIST_large'
root_01=os.path.splitext(path_01)
root_02=os.path.splitext(path_02)
print(root_01)
print(root_02)
print(root_01[0])
(‘D:/User/wgy/workplace/data/notMNIST_large.tar’, ‘.gar’)
(‘D:/User/wgy/workplace/data/notMNIST_large’, ‘’)
D:/User/wgy/workplace/data/notMNIST_large.tar
os.path.basename()
os.path.basename(),返回path最后的文件名。若path以/或\结尾,那么就会返回空值。
代码示例
import os
path_01='D:/User/wgy/workplace/data/notMNIST_large.tar.gar'
path_02='D:/User/wgy/workplace/data/notMNIST_large'
root_01=os.path.splitext(path_01)
root_02=os.path.splitext(path_02)
print(root_01)
print(root_01[0])
basename = os.path.basename(root_01[0])
print(basename)
(‘D:/User/wgy/workplace/data/notMNIST_large.tar’, ‘.gar’)
D:/User/wgy/workplace/data/notMNIST_large.tar
notMNIST_large.tar