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 · 2567 次点击
    这是一个创建于 1814 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    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 有什么满足不了吗?
    shuax
        3
    shuax  
       2019-05-07 12:02:23 +08:00
    过早优化是万恶之源。能用就行
    Abbeyok
        4
    Abbeyok  
       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 命令嚒
    lc1450
        8
    lc1450  
       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 不能做,而这段代码能做的
    lc1450
        13
    lc1450  
       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
    lc1450
        16
    lc1450  
       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   ·   我们的愿景   ·   实用小工具   ·   5773 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 02:33 · PVG 10:33 · LAX 19:33 · JFK 22:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.