欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

python中关于前后缀操作的详解

程序员文章站 2022-05-09 14:58:07
...


返回后缀名

import os

path = 'first_directory/second_directory/file.txt'print os.path.splitext(path)[1]print type(os.path.splitext(path)[1])
.txt
<type 'str'>

前缀名的bool判断

path = 'first_directory/second_directory/file.txt'print path.startswith('fir')
True

后缀名的bool判断

path = 'first_directory/second_directory/file.txt'print path.endswith('.txt')
True

os.path.splitext、.startswith、.startswith 探究

import os

path = 'first_directory/second_directory/file.txt'print os.path.splitext(path)print os.path.splitext(path)[1]print type(os.path.splitext(path)[1])printprint path.startswith('fir')print path.startswith('aaa')print type(path.startswith('fir'))printprint path.endswith('.txt')print path.endswith('.aaa')print type(path.endswith('.txt'))
('first_directory/second_directory/file', '.txt')
.txt
<type 'str'>

True
False
<type 'bool'>

True
False
<type 'bool'>

以上就是python中关于前后缀操作的详解的详细内容,更多请关注其它相关文章!