首頁 >後端開發 >Python教學 >python中關於前後綴運算的詳解

python中關於前後綴運算的詳解

黄舟
黄舟原創
2017-10-07 11:40:582051瀏覽


傳回後綴名

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