startswith和endswith
1.在開頭和結尾進行單種前綴或後綴的匹配
>>>filename = 'foo.txt'
>>>filename.endswith('.txt')
True
>>>filename.startswith('foo1')
False
2.如果需要對多個選項的前綴或後綴進行匹配,提供一個元祖參數即可
必須是元祖,如果是列表或集合,使用tuple()進行轉換
>>>import os
>>>file_list = os.listdir('.')
>>>file_list
['README.md', 'form.png', 'test.txt', 'export_patrol_result.csv', 'logs.c', 'manage.py']
>>>[name for name in file_list if name.endswith(('.md', '.png', '.py'))]
['README.md', 'form.png', 'manage.py']