首页 > 编程 > Python > 正文

python去除扩展名的实例讲解

2020-02-22 23:50:27
字体:
来源:转载
供稿:网友

获取不带扩展名的文件的名称:

import osprintos.path.splitext("path_to_file")[0] from os.path import basename# now you can call it directly with basenameprint 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])akarmidef 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

以上这篇python去除扩展名的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林站长站。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表