首页  >  文章  >  后端开发  >  python中关于前后缀操作的详解

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

黄舟
黄舟原创
2017-10-07 11:40:582030浏览


返回后缀名

import os

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

前缀名的bool判断

path = &#39;first_directory/second_directory/file.txt&#39;print path.startswith(&#39;fir&#39;)
True

后缀名的bool判断

path = &#39;first_directory/second_directory/file.txt&#39;print path.endswith(&#39;.txt&#39;)
True

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

import os

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

True
False


True
False

以上是python中关于前后缀操作的详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn