美文网首页
Python 查找Linux文件

Python 查找Linux文件

作者: WANGJ01 | 来源:发表于2018-12-06 17:26 被阅读0次

通过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文件

      本文链接:https://www.haomeiwen.com/subject/aenocqtx.html