[ Python ]递归遍历文件夹,使用生成器返回每个文件名_md 版本 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
AILOVEU
V2EX    Python

[ Python ]递归遍历文件夹,使用生成器返回每个文件名_md 版本

  •  
  •   AILOVEU 2019-05-07 11:13:01 +08:00 3230 次点击
    这是一个创建于 2405 天前的主题,其中的信息可能已经有所发展或是发生改变。

    贴一段自己写的代码,主要实现以下功能:

    1. 递归遍历源目录( src_path )下的文件,可以用于检测文件
    2. 可以将源目录下的所有文件处理后传到目标目录(det_path)中,即可以对源文件批量处理,将 print 替换成对应方法即可
    import os def dir_walk(src_path,det_path=None): if os.path.isdir(src_path): if det_path and not os.path.isdir(det_path): os.makedirs(det_path) for name in os.listdir(src_path): _src_path = os.path.join(src_path,name) _det_path = det_path and os.path.join(det_path,name) yield from dir_walk(_src_path,_det_path) if os.path.isfile(src_path): yield src_path,det_path for i,_ in dir_walk('/home'): print(i) 

    主要有几个疑问,大家帮忙试着解答下哈:

    1. 代码的执行效率如何,递归时使用 yield 会不会没有发挥 yield 的作用
    2. yield from 和递归一起使用感觉怪怪的,这是我试出来的,这样写竟然可以用,谁帮忙解释下怎么的语法啊
    3. 为了让方法可以同时满足我上面说的两种功能,所以加的第二个参数默认为 None,然后方法里一些判断,当然这样就降低了代码的效率,有更好的解决办法吗?
    17 条回复    2019-05-07 14:07:02 +08:00
    AILOVEU
        1
    AILOVEU  
    OP
       2019-05-07 11:20:53 +08:00
    这个帖子上去啊
    neoblackcap
        2
    neoblackcap  
       2019-05-07 11:29:34 +08:00
    os.walk 有什么满足不了吗?
    momo1999
        3
    momo1999  
       2019-05-07 12:02:23 +08:00
    过早优化是万恶之源。能用就行
    Ct5T66PVR1bW7b2z
        4
    Ct5T66PVR1bW7b2z  
       2019-05-07 12:09:53 +08:00 via Android
    递归就是坑坑坑坑
    AILOVEU
        5
    AILOVEU  
    OP
       2019-05-07 12:21:37 +08:00
    @neoblackcap 不能复制到新的文件夹下,它只能遍历
    liukrystal
        6
    liukrystal  
       2019-05-07 12:43:49 +08:00
    为什么不用 os.walk()而是自己造轮子...
    jeadong
        7
    jeadong  
       2019-05-07 12:52:27 +08:00
    知道 dir 命令
    atx
        8
    atx  
       2019-05-07 13:20:14 +08:00
    python3.5 及以上用 os.scandir 会更快一点
    AILOVEU
        9
    AILOVEU  
    OP
       2019-05-07 13:24:49 +08:00
    @liukrystal 不光是遍历,主要是第二个参数,可以用于复制文件等
    AILOVEU
        10
    AILOVEU  
    OP
       2019-05-07 13:29:19 +08:00
    @jeadong 什么啊?
    AILOVEU
        11
    AILOVEU
    OP
       2019-05-07 13:36:43 +08:00
    @lc1450 是指替换 os.listdir 吧
    casparchen
        12
    casparchen  
       2019-05-07 13:39:37 +08:00 via iPad
    没看懂其中有什么是 os.walk 不能做,而这段代码能做的
    atx
        13
    atx  
       2019-05-07 13:39:45 +08:00
    @AILOVEU 和 os.walk()差不多, 具体可以看官方文档
    AILOVEU
        14
    AILOVEU  
    OP
       2019-05-07 13:40:33 +08:00
    如果不用 yield,递归是很容易实现的,因为最近学到了,所以就想把它包装成一个方法; os.walk 也可以勉强完成处理后复制文件的功能,但用起来会很别扭
    AILOVEU
        15
    AILOVEU  
    OP
       2019-05-07 13:41:55 +08:00
    @lc1450 Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type or file attribute information
    atx
        16
    atx  
       2019-05-07 13:57:49 +08:00
    @AILOVEU
    不好意看错了 os.walk 文档下面解释 Changed in version 3.5: This function now calls os.scandir() instead of os.listdir(), making it faster by reducing the number of calls to os.stat().

    scandir 和 walk 一般都直接用来迭代目录,还以为这俩是一样的
    AILOVEU
        17
    AILOVEU  
    OP
       2019-05-07 14:07:02 +08:00
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     833 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 21:21 PVG 05:21 LAX 13:21 JFK 16:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86