Python 官方文档:入门教程 => 点击学习
获取不带扩展名的文件的名称: import os printos.path.splitext("path_to_file")[0] from os.path import basename # now you can call
获取不带扩展名的文件的名称:
import os
printos.path.splitext("path_to_file")[0]
from os.path import basename
# now you can call it directly with basename
print basename("/a/b/c.txt")
>>>base=os.path.basename('/root/dir/sub/file.ext')
>>> base
'file.ext'
>>> os.path.splitext(base)
('file', '.ext')
>>> os.path.splitext(base)[0]
'file'
>>>
>>> printos.path.splitext(os.path.basename("hemanth.txt"))[0]
hemanth
>>> file ='/root/dir/sub.exten/file.data.1.2.dat'
>>> print('.').join(file.split('.')[:-1])
/root/dir/sub.exten/file.data.1.2
>>> s = 'c:\\temp\\akarmi.txt'
>>> print(os.path.splitext(s)[0])
c:\temp\akarmi
因此,我不需要驱动器号或者目录名,我使用:
>>>print(os.path.splitext(os.path.basename(s))[0])
akarmi
def getFileNameWithoutExtension(path):
returnpath.split('\\').pop().split('/').pop().rsplit('.', 1)[0]
getFileNameWithoutExtension('/path/to/file-0.0.1.ext')
# => file-0.0.1
getFileNameWithoutExtension('\\path\\to\\file-0.0.1.ext')
# => file-0.0.1
--结束END--
本文标题: python去除扩展名
本文链接: https://www.lsjlt.com/news/189982.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0