1. 程式人生 > >Flask原始碼解析:字串方法endswith與os.path.splittext()

Flask原始碼解析:字串方法endswith與os.path.splittext()

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():返回檔案的路徑和檔名構成的元組