import os, sys

 def search(curpath, s):
L = os.listdir(curpath) #列出当前目录下所有文件
for subpath in L: #遍历当前目录所有文件
if os.path.isdir(os.path.join(curpath, subpath)): #若文件仍为目录,递归查找子目录
newpath = os.path.join(curpath, subpath)
search(newpath, s)
elif os.path.isfile(os.path.join(curpath, subpath)): #若为文件,判断是否包含搜索字串
if s in subpath:
print os.path.join(curpath, subpath) def main():
workingpath = os.path.abspath('.')
s = sys.argv[1]
search(workingpath, s) if __name__ == '__main__':
main()

PS: 关键分析红字部分

  如果直接使用 subpath ,因它只是一个文件名,故判断它是否为目录语句 os.path.isdir(subpath) 只会在当前目录下查找subpath文件;而不会随着search的递归而自动在更新路径下查找。比如:

+/home/freyr/
|
|--------dir1/
|            |
|            |-------file1
|            |-------file2
|
|--------dir2/
|
|--------file2

Step1、在主目录下遍历,subpath = dir1时,先判断是否为目录:os.path.isdir(subpath)其实就是os.path.isdir('/home/freyr/dir1')

Step2、dir1为目录,遍历dir1。subpath = file1时,同样先判断是否为目录:os.path.isdir(subpath)其实是os.path.isdir('/home/freyr/file1'),而不是os.path.isdir('/home/freyr/dir1/file1'),很明显/home/freyr下没有file1文件。这样造成的后果就是除了当前目录(一级目录)文件(如file2)可以搜索到,子目录内文件都是没有搜索到的,因为它每次都是跑到/home/freyr下搜索文件名

  其实简单的说,os.path.isdir()函数在这里使用绝对路径!

Python实现Linux下文件查找的更多相关文章

  1. Linux下文件查找命令find笔记

    在Linux命令下如果需要快速自己系统所需要处理的文件,可以通过find命令快速进行检索. 如果想在某个路径下查找相应的文件可以执行如下命令: find path -name filename # p ...

  2. linux下文件查找工具--find

    常用的文件查找命令有:which,locate,find 1.which命令 查找二进制数或二进制命令,由PATH给出 2.loacte 特点: 1.非实时,每天在系统上生成数据库,通过数据库查询 2 ...

  3. python实现linux下文件遍历

    import os def getAllFile(*names): if len(names) == 0: return "" else: allList = [] for nam ...

  4. Linux下快速查找文件

    1 locate 查找内容.查找数据库,updatedb命令更新数据库 2 which 命令 3 find 路径 -name 查找内容.find命令会磁盘查找,比较耗时. 4 grep 查找内容一般为 ...

  5. Linux下文件搜索、查找、查看命令

    Linux下文件搜索.查找.查看命令 1.最强大的搜索命令:find 查找各种文件的命令 2.在文件资料中查找文件:locate 3.搜索命令所在的目录及别名信息:which 4.搜索命令所在的目录及 ...

  6. Linux操作系统的文件查找工具locate和find命令常用参数介绍

    Linux操作系统的文件查找工具locate和find命令常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.非实时查找(数据库查找)locate工具  locate命 ...

  7. Linux下如何查找可执行文件

    Linux下的可执行文件 Linux下如何查找可执行文件,作为一个Linux小菜刚刚有了这个问题, 在windows中,可以通过后缀名判断是否是可执行文件,比如.exe,.bat等是可执行文件,但是在 ...

  8. Windows与Linux下文件操作监控的实现

    一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...

  9. linux 批量文件查找并替换

    linux 批量文件查找并替换 sed -i "s/oldstring/newstring/g" `grep oldstring -rl path` 如: sed -i " ...

随机推荐

  1. Use Spring Insight Developer to Analyze Code, Install it with Tomcat, and Extend it with Plugins--转载

    原文地址:http://www.tomcatexpert.com/blog/2012/12/05/use-spring-insight-developer-analyze-code-install-i ...

  2. 关于Android LayoutInflater的解释

    LayoutInflater的作用就是动态加载xml布局好的界面,类似于findViewById()来获取已经定义好的控件一样.不同点是LayoutInflater是用来找res/layout/下的x ...

  3. C# 自定义重绘TextBox

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. UVa OJ 10055

    Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...

  5. cplusplus系列>algorithm>std::for_each

    http://www.cplusplus.com/reference/algorithm/for_each/ 对一个序列应用函数.可以是函数指针,或者是functor. // for_each exa ...

  6. Android&iOS崩溃堆栈上报

    Android&iOS崩溃堆栈上报 原文地址:http://www.cnblogs.com/songcf/p/4885468.html 通过崩溃捕获和收集,可以收集到已发布应用(游戏)的异常, ...

  7. Android(java)学习笔记64:线程的控制

    1. 线程休眠: Java中线程休眠指让正在运行的线程暂停执行一段时间,进入阻塞状态,通过调用Thread类的静态方法sleep得以实现. 当线程调用sleep进入阻塞状态后,在其休眠的时间内,该线程 ...

  8. ArcEngine实现捕捉节点

    来自:http://blog.sina.com.cn/s/blog_4d0b75870100o960.html //获取最近的结点,然后在  OnMouseMove中显示 //pnt:鼠标移动点 // ...

  9. Java学习笔记——显示当前日期的三种方式

    一.Date类:这是一种过时的表达方式 import java.util.Date; Date date = new Date(); System.out.println((1900+date.get ...

  10. Shell学习笔记 - 循环语句

    一.for循环 1. 语法格式1 for 变量 in 值1 值2 值3 ... do 程序 done 说明:程序将遍历所有的值,赋值给变量,然后在执行程序.也就是说,后面接多少个值,程序就循环多少次. ...