startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。

endswith() 方法用于检查字符串是否是以指定子字符串结尾,如果是则返回 True,否则返回 False。可以用来判断文件的扩展名

file = '1.txt'
if file.startswith('1'):
    print ('文件以1开头')
if file.endswith('.txt'):
    print('txt文件')