zoukankan      html  css  js  c++  java
  • python 遍历目录 正则

    20.5.101.31_/root>ls -ltr 10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-*
    -rw-r--r-- 1 root root 43206314 9月  20 18:44 10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000106.log.1.txt
    -rw-r--r-- 1 root root 86572354 9月  20 18:49 10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000106.log.1.pro
    -rw-r--r-- 1 root root    58030 9月  20 18:49 10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000106.log.1.fin
    -rw-r--r-- 1 root root    58030 9月  21 09:38 10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000106.log.1.fin1
    -rw-r--r-- 1 root root    58030 9月  21 09:38 10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000111.log.fin
    
    
    20.5.101.31_/root>cat a1.py
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import os
    import re
    import sys
    import time
    aaa=[]
    def listdir(path, list_name): #传入存储的list
     for file in os.listdir(path): 
       file_path = os.path.join(path, file) 
       if os.path.isdir(file_path): 
         listdir(file_path, list_name) 
       else: 
          list_name.append(file_path)
    listdir('/esb/logdir',aaa)
    print type(aaa)
    bbb=[]
    for x in aaa:
      if (re.search("fin$",x)):
         if (re.search('10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-',x)):
            print x
            bbb.append(x)
    print bbb
    print type(bbb)
    
    
    20.5.101.31_/root>python a1.py
    <type 'list'>
    /esb/logdir/10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000106.log.1.fin
    /esb/logdir/10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000111.log.fin
    ['/esb/logdir/10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000106.log.1.fin', '/esb/logdir/10106020180920010040173762-esbapp3-esb_in-20180920171404-405283-1300200000111.log.fin']
    <type 'list'>
  • 相关阅读:
    NGINX-HTTPS
    README
    SSH
    Ubuntu
    Python复利
    Python全双工聊天
    Python半双工聊天
    Python网络编程
    使用Python PIL库中的Image.thumbnail函数裁剪图片
    Python模块 os.walk
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349050.html
Copyright © 2011-2022 走看看