1、字符串方法endswith
endswith方法:
def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__ """ S.endswith(suffix[, start[, end]]) -> bool Return True if S ends with the specified suffix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. suffix can also be a tuple of strings to try. """ return False
其中suffix支持字符串構成的元組(tuple)。
filename = "1.html"
filename = "1.xml"
# def endswith(self, suffix, start=None, end=None):
# suffix can also be a tuple of strings to try.
r = filename.endswith(('.html', '.htm', '.xml', '.xhtml'))
print(r)
輸出結果為:False
2、os.path.splittext
import os
fn = "F:/2018/python/review/project/Flask_01/__get__與__set__.py"
# os.path.splitext
# f: __get__與__set__.py
f = os.path.basename(fn)
print(f)
# r: ('__get__與__set__', '.py')
r = os.path.splitext(f)
print(r)
# os.path.split
dirname,filename=os.path.split('/home/ubuntu/python_coding/split_func/split_function.py')
# dirname: /home/ubuntu/python_coding/split_func
# filename: split_function.py
print(dirname, filename)
os.path.splittext():將文件名和擴展名分開,返回由文件名和擴展名構成的元組
os.path.split():返回文件的路徑和文件名構成的元組
