当前位置:Gxlcms > Python > python中关于前后缀操作的详解

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

时间:2021-07-01 10:21:17 帮助过:86人阅读


返回后缀名

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中关于前后缀操作的详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行