Python 查找Linux文件

通过python os 模块,遍历目录中指定后缀名的文件。

脚本内容如下:

执行结果:


#!/usr/bin/python

#encoding:utf-8

import os

FileList=[]

def ScanFile(Dir,Suffix):

    if os.path.isdir(Dir):

      items=os.listdir(Dir)

      for names in items:

        if os.path.isfile(Dir+'/'+names) and names.endswith(Suffix):

          FileList.append(Dir+'/'+names)

        else:

          if os.path.isdir(Dir+'/'+names):

            #print Dir+'/'+names

            ScanFile(Dir+'/'+names,Suffix)

DIRNAME="/tmp"

ScanFile(DIRNAME,".log")

if len(FileList)!=0:

  print(FileList)

else:

  print("查找文件不存在")

你可能感兴趣的:(Python 查找Linux文件)