本文摘自php中文网,作者黄舟,侵删。
返回后缀名
1 2 3 | import os
path = 'first_directory/second_directory/file.txt' print os.path.splitext(path)[ 1 ] print type (os.path.splitext(path)[ 1 ])
|
前缀名的bool判断
1 | path = 'first_directory/second_directory/file.txt' print path.startswith( 'fir' )
|
后缀名的bool判断
1 | path = 'first_directory/second_directory/file.txt' print path.endswith( '.txt' )
|
os.path.splitext、.startswith、.startswith 探究
1 2 3 | 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' ))
|
1 2 3 4 5 6 7 8 9 10 11 | ( 'first_directory/second_directory/file' , '.txt' )
.txt
< type 'str' >
True
False
< type 'bool' >
True
False
< type 'bool' >
|
以上就是python中关于前后缀操作的详解的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python内建数据结构详解
Python需要有编程的基础吗
一份Python入门应该看的学习资料
Python中count函数的用法
Python如何输入中文
Python分段函数如何编写?
Python运算符-如何在实战中使用赋值运算符实例
Python字典怎么根据值返回键
Python中del函数的用法详解
Python中根据字符串导入模块module的方法介绍(附代码)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中关于前后缀操作的详解