import osimport fnmatchfor filename in os.listdir('.'): if fnmatch.fnmatch(filename, '*.txt'): # 匹配模式为星号,表示任意的字符 os.remove(filename)