python去除扩展名的实例讲解
获取不带扩展名的文件的名称:
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 相关推荐
ljbhander 2020-06-14
zhangskd 2020-04-18
揅笑晏晏 2010-10-14
futurezone 2014-10-17
sufwei 2012-11-09
留百叶 2011-08-03
manifold 2014-11-12
internet我最爱 2014-11-12
xiaoxin0 2010-04-22
Mrsandman 2018-01-14
lcwben 2010-12-16
Tomhsfreestyle 2013-10-30
VincentDrW 2007-11-19
Julyth 2019-06-13
Huangguohao 2019-06-13
流离岁月 2019-06-13