亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2170 | 回復(fù): 7
打印 上一主題 下一主題

遞歸匹配文件 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2014-12-09 17:50 |只看該作者 |倒序?yàn)g覽
  1. tmp/
  2. ├── a.tpl
  3. ├── tmp
  4. │   ├── b.tpl
  5. │   ├── tmp
  6. │   │   ├── c.tpl
  7. │   │   └── tmp
  8. │   │       └── d.tpl
  9. │   └── yy
  10. └── xx
復(fù)制代碼
希望得到
  1. ["./tmp/a.tpl","./tmp/tmp/tmp/tmp/d.tpl","./tmp/tmp/tmp/c.tpl","./tmp/tmp/b.tpl"]
復(fù)制代碼

論壇徽章:
1
2015七夕節(jié)徽章
日期:2015-08-21 17:58:43
2 [報告]
發(fā)表于 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()]
復(fù)制代碼
lol

論壇徽章:
2
白羊座
日期:2014-06-17 11:04:28午馬
日期:2014-12-29 15:37:13
3 [報告]
發(fā)表于 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
復(fù)制代碼
試試

論壇徽章:
0
4 [報告]
發(fā)表于 2014-12-10 13:08 |只看該作者
好吧,有更pythoner的實(shí)現(xiàn)嗎?回復(fù) 2# shreychen


   

論壇徽章:
1
2015七夕節(jié)徽章
日期:2015-08-21 17:58:43
5 [報告]
發(fā)表于 2014-12-10 13:31 |只看該作者
回復(fù) 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"])
復(fù)制代碼
結(jié)果
  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']
復(fù)制代碼

論壇徽章:
1
2015七夕節(jié)徽章
日期:2015-08-21 17:58:43
6 [報告]
發(fā)表于 2014-12-10 13:50 |只看該作者
其實(shí)用os.walk()很容易實(shí)現(xiàn),既然樓主要遞歸就遞歸吧.回復(fù) 5# shreychen


   

論壇徽章:
0
7 [報告]
發(fā)表于 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]#

復(fù)制代碼

論壇徽章:
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運(yùn)維版塊每日發(fā)帖之星
日期:2015-12-05 06:20:00神斗士
日期:2015-12-03 23:13:59IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-11-22 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-11-08 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-10-29 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-06-30 06:20:00
8 [報告]
發(fā)表于 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)
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP