python去除扩展名的实例讲解
作者:ShellCollector 时间:2022-05-08 18:10:49
获取不带扩展名的文件的名称:
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
来源:https://blog.csdn.net/jacke121/article/details/76685759
标签:python,扩展名
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
为博客增加社会化分享按钮代码
2010-01-31 16:52:00
Appium+Python自动化测试之运行App程序示例
2023-07-29 01:54:20
![](https://img.aspxhome.com/file/2023/6/60986_0s.jpg)
python实现复制大量文件功能
2023-09-03 16:42:54
网页代码更清晰高效的一些经验
2008-05-19 12:23:00
有序列表 li ol
2008-07-30 12:31:00
自动定时备份sqlserver数据库的方法
2011-11-03 17:28:17
以独占方式打开Access数据库
2007-10-22 12:24:00
Django 跨域请求处理的示例代码
2022-05-27 17:08:46
![](https://img.aspxhome.com/file/2023/3/65503_0s.png)
Python元组定义及集合的使用
2023-11-22 12:32:03
如何快速通过XSL转换XML文件
2023-07-02 21:22:40
符合标准的纯CSS三 级弹出菜单
2008-01-06 15:34:00
![](https://img.aspxhome.com/file/UploadPic/20081/6/200816154232905s.gif)
SQL Server上进行表设计时表的主键设计问题
2010-06-24 16:10:00
PHP垃圾回收机制超详细介绍
2023-11-21 23:11:44
![](https://img.aspxhome.com/file/2023/4/106324_0s.png)
浅谈Scrapy框架普通反爬虫机制的应对策略
2023-07-14 17:11:40
什么是响应式Web设计?怎样进行?
2011-11-21 17:00:40
asp xmlhttp下载一句话
2013-06-30 06:47:48
做了CDN加速的ASP网站获取用户真实IP程序
2011-02-16 10:59:00
微信公众平台开发教程③ PHP实现微信公众号支付功能图文详解
2023-11-18 19:09:27
![](https://img.aspxhome.com/file/2023/9/102089_0s.png)
Oracle DECODE函数语法使用介绍
2023-07-17 12:44:22
认识Javascript数组
2009-08-27 15:26:00