zoukankan      html  css  js  c++  java
  • Find cmd

    Find cmd with python programing python at page320

    Unix find cmd:

    find . -name "*.py " -print -exec fgrep popen{};

     1 from glob import glob
     2 import os
     3 
     4 def findWithExt(type):
     5     for file in glob(type):
     6         if 'xlian7164584' in open(file).read():
     7             print(file)
     8             print('current directory is %s' % os.path.abspath('.'))
     9             
    10 ext='*.*'
    11 findWithExt(ext)
    View Code

    find cmd:

     1 """
     2 return all files matching a filename patten at  and below a root directory;
     3 
     4 custom version of the now dreprecated find modelue in the standard lib:
     5 imprt as "Tools.find" like original ,but uses os.walk loop,has no support for pruning subdires,and
     6 is runnable as top-level script;
     7 
     8 find() is a generator that uses the os.walk() generator to yield just
     9 mathing filenames:use findlist() to forse result list generation;
    10 """
    11 import fnmatch
    12 from glob import glob
    13 import os
    14 
    15 def find(pattern,startdir=os.curdir):
    16     for (thisDir,subHere,filesHere) in os.walk(startdir):
    17         for name in subHere +filesHere:
    18             if fnmatch.fnmatch(name,pattern):
    19                 fullpath = os.path.join(thisDir,name)
    20                 yield fullpath
    21 
    22 def findlist(pattern,startdir=os.curdir,dosort=False):
    23     matches = list(find(pattern,startdir))
    24     if dosort:
    25         mathches.sort()
    26     return matches
    27 
    28 
    29 def findWithExt(type):
    30     for file in glob(type):
    31         if 'xlian7164584' in open(file).read():
    32             print(file)
    33             print('current directory is %s' % os.path.abspath('.'))
    34             
    35 #ext='*.*'
    36 #findWithExt(ext)
    37 
    38 if __name__ =='__main__':
    39     import sys
    40     namepattern,startdir = sys.argv[1],sys.argv[2]
    41     for name in find(namepattern,startdir):
    42         print(name)
    View Code

  • 相关阅读:
    几个 vim 的块操作命令
    图灵社区 : 阅读 : 谁说Vim不是IDE?(三)
    google.sg
    Vim 配置详解_wuyang
    Vim 配置详解_wuyang
    不忘本~结构
    刚刚做了一个菜单导航变亮的效果,共享一下吧!
    不忘本~静态构造函数
    数据结构~时间复杂度和空间复杂度
    数据结构~在页面上渲染树型结构
  • 原文地址:https://www.cnblogs.com/lxk613/p/4836817.html
Copyright © 2011-2022 走看看