免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2112 | 回复: 7
打印 上一主题 下一主题

递归匹配文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-12-09 17:50 |只看该作者 |倒序浏览
  1. tmp/
  2. ├── a.tpl
  3. ├── tmp
  4. │   ├── b.tpl
  5. │   ├── tmp
  6. │   │   ├── c.tpl
  7. │   │   └── tmp
  8. │   │       └── d.tpl
  9. │   └── yy
  10. └── xx
复制代码
希望得到
  1. ["./tmp/a.tpl","./tmp/tmp/tmp/tmp/d.tpl","./tmp/tmp/tmp/c.tpl","./tmp/tmp/b.tpl"]
复制代码

论坛徽章:
1
2015七夕节徽章
日期:2015-08-21 17:58:43
2 [报告]
发表于 2014-12-09 18:58 |只看该作者
  1. def search_files(pattern, search_path):
  2.     cmd = "find {path} -type f -name {pattern}".format(path=search_path, pattern=pattern)
  3.     return [f.strip() for f in os.popen(cmd).readlines()]
复制代码
lol

论坛徽章:
2
白羊座
日期:2014-06-17 11:04:28午马
日期:2014-12-29 15:37:13
3 [报告]
发表于 2014-12-10 10:43 |只看该作者
  1. import os
  2. l = []
  3. def Dir(indir):
  4.     for i in os.listdir(indir):
  5.         if os.path.isdir(indir,i):
  6.             return Dir(os.path.join(indir,i))
  7.         else:
  8.             l.append(os.path.join(indir,i))
  9. Dir('.')
  10. print l
复制代码
试试

论坛徽章:
0
4 [报告]
发表于 2014-12-10 13:08 |只看该作者
好吧,有更pythoner的实现吗?回复 2# shreychen


   

论坛徽章:
1
2015七夕节徽章
日期:2015-08-21 17:58:43
5 [报告]
发表于 2014-12-10 13:31 |只看该作者
回复 4# __daydayup__
  1. #!/usr/bin/env python
  2. #coding:utf-8

  3. import os,glob

  4. def search_files(pattern,search_path):
  5.     if not search_path:
  6.         return []
  7.     childpathes = []
  8.     matches = []
  9.     for spath in search_path:
  10.         matches += [os.path.join(spath,f) for f in glob.glob1(spath, pattern) if os.path.isfile(os.path.join(spath, f)) ]
  11.         for p in os.listdir(spath):
  12.             childpath = os.path.join(spath, p)
  13.             if os.path.isdir(childpath):
  14.                 childpathes.append(childpath)
  15.     return matches + search_files(pattern, childpathes)


  16. print search_files("*.tpl", ["/tmp/tmp"])
复制代码
结果
  1. $ python search.py
  2. ['/tmp/tmp/a.tpl', '/tmp/tmp/tmp/b.tpl', '/tmp/tmp/tmp/tmp/c.tpl', '/tmp/tmp/tmp/tmp/tmp/d.tpl']
复制代码

论坛徽章:
1
2015七夕节徽章
日期:2015-08-21 17:58:43
6 [报告]
发表于 2014-12-10 13:50 |只看该作者
其实用os.walk()很容易实现,既然楼主要递归就递归吧.回复 5# shreychen


   

论坛徽章:
0
7 [报告]
发表于 2014-12-11 22:09 |只看该作者
6楼给出了最方便的思路,很正确,看下简单代码:
  1. [root@localhost tmp]# python a.py
  2. ['/tmp/temp/a.tpl', '/tmp/temp/tmp/b.tpl', '/tmp/temp/tmp/tmp/c.tpl', '/tmp/temp/tmp/tmp/tmp/d.tpl']
  3. [root@localhost tmp]# cat a.py
  4. #!/usr/bin/env python
  5. import os
  6. a=[]
  7. for root,dirs,files in os.walk("/tmp/temp/"):
  8.         for file in files:
  9.                 f=os.path.join(root,file)
  10.                 a.append(f)

  11. print a
  12. [root@localhost tmp]#

复制代码

论坛徽章:
12
2015年亚洲杯之乌兹别克斯坦
日期:2015-04-10 18:29:00狮子座
日期:2016-01-17 15:40:28处女座
日期:2016-01-16 17:36:17巨蟹座
日期:2016-01-16 17:35:48未羊
日期:2015-12-12 16:18:26青铜圣斗士
日期:2015-12-09 01:07:50IT运维版块每日发帖之星
日期:2015-12-05 06:20:00神斗士
日期:2015-12-03 23:13:59IT运维版块每日发帖之星
日期:2015-11-22 06:20:00IT运维版块每日发帖之星
日期:2015-11-08 06:20:00IT运维版块每日发帖之星
日期:2015-10-29 06:20:00IT运维版块每日发帖之星
日期:2016-06-30 06:20:00
8 [报告]
发表于 2014-12-16 11:21 |只看该作者
使用walk 完成的并将输出写到文件中去了
# _*_encoding:utf-8 _*_
from os.path import walk  
m='C:\\Users\\User\\Desktop\\test'
filenamelist=[]
def visit(arg,dirname,names,flist=filenamelist):
    f=open('C:\\tt.txt','w')
    flist += [dirname + '\\'+ file for file in names]
#     for i  in   flist[:]:
    for i in range(0,len(flist)):
        f.write(flist[i])
    print flist
    f.close()
walk(m,visit,0)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP