python获取指定目录下的所有指定后缀的文件名

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

获取指定目录下的所有指定后缀的文件名

使用到的函数有: 
os.path.splitext():分离文件名与扩展名

代码如下:

#! /usr/bin/python# -*- coding: utf-8 -*-import osdef getFileName(path):    ''' 获取指定目录下的所有指定后缀的文件名 '''    f_list = os.listdir(path)    # print f_list    for i in f_list:        # os.path.splitext():分离文件名与扩展名        if os.path.splitext(i)[1] == '.log':            print iif __name__ == '__main__':    path = '/home/xx/work/ETS/log/1/1'    getFileName(path)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

执行结果如下:

1429761218.log1429761376.log1429761162.log1429761249.log1429761208.log1429755686.log1429761294.log1429761203.log1429755747.log1429761269.log1429755737.log1429761228.log1429755717.log1429761254.log1429761157.log1429761284.log1429761330.log1429761355.log1429761274.log1429761350.log1429761142.log1429761152.log1429761325.log1429761259.log1429761239.log1429761340.log1429761300.log1429761213.log1429761244.log1429761335.log1429755757.log1429761366.log1429761289.log1429761345.log1429755676.log1429761360.log1429761279.log1429755697.log1429761371.log1429761188.log1429761167.log1429761223.log1429755727.log1429761315.log1429761264.log1429761310.log1429761183.log1429755707.log1429761147.log1429761320.log1429761233.log1429761305.log
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ggjttfc/article/details/83834239