本文原创,版权属作者个人所有,如需转载请联系作者本人。Q&微:155122733
--------------------------------------------------------------------------------------------------------
直接上代码吧,三种方式,如下:
#!/usr/bin/python ''' 显示所有文件的后缀 ''' #方式一: filenames = ['1.py','2.py','3.txt'] for index in range(0, len(filenames)): indextmp = filenames[index].rfind('.') print(filenames[index][indextmp:]) #方式二 注意:列表filenames中元素是什么类型,filename就是什么类型,可以用type(filename)查看,该用例中所有元素都为str类型,所以才能够调用.rfind()函数 for filename in filenames: indextmp = filename.rfind('.') print(filename[indextmp:]) #方式三 index = 0 while index < len(filenames): indextmp = filenames[index].rfind('.') print(filenames[index][indextmp:]) index+=1