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,扩展名
0
投稿

猜你喜欢

  • css实现简单圆角效果

    2008-11-27 13:11:00
  • Variant总能找到与之相匹配的数据类型吗?

    2009-10-29 12:20:00
  • Jenkins部署war包和部署jar包的详细步骤

    2023-01-19 07:13:06
  • window环境下使用VScode连接虚拟机MySQL方法

    2024-01-21 04:42:06
  • 关于数据库中保留小数位的问题

    2024-01-13 13:34:45
  • mysql中limit的用法深入分析

    2024-01-21 17:28:17
  • FrontPage XP设计教程5——表单的设计

    2008-10-11 12:35:00
  • Python用函数思想完成哥德巴赫猜想代码分析

    2022-05-05 11:42:06
  • SQL Server数据库分离和附加数据库的操作步骤

    2024-01-27 19:59:23
  • Linux oracle数据库自动备份自动压缩脚本代码

    2024-01-19 01:44:40
  • Python实现多并发访问网站功能示例

    2022-12-16 11:42:47
  • ASP从数据库中获取下载文件

    2007-10-06 21:17:00
  • 一个jquery日期选取插件源码

    2009-12-23 19:15:00
  • 用python实现将数组元素按从小到大的顺序排列方法

    2022-01-07 22:03:25
  • ASP中Cookies集合使用方法详解

    2007-09-14 10:16:00
  • Python NumPy教程之二元计算详解

    2023-12-16 18:25:04
  • Python深度强化学习之DQN算法原理详解

    2023-03-05 12:02:51
  • python Selenium 库的使用技巧

    2021-08-07 21:23:47
  • Python venv虚拟环境配置过程解析

    2021-11-30 20:21:48
  • vue实现输入框的模糊查询的示例代码(节流函数的应用场景)

    2024-05-08 09:33:35
  • asp之家 网络编程 m.aspxhome.com