zoukankan      html  css  js  c++  java
  • python 文件夹os.walk

    测试的目录结构如下:

    .
    ├── 1.txt
    ├── 2.txt
    ├── a
    │   ├── 3.txt
    │   ├── d
    │   └── e
    ├── b
    │   ├── 4.txt
    │   ├── 7.txt
    │   ├── f
    │   │   └── 5.txt
    │   └── h
    └── c
        └── 5.txt
    
    7 directories, 7 files
    

    代码如下:

    import os
    
    root_dir = "/data_1/everyday/0723/test/"
    
    for root, dir, files in os.walk(root_dir):
            for file in files:
                print(root)
                print(dir)
                print(file)
                print("--------"*10)
                print()
                print()
    

    输出如下:

    /data_1/everyday/0723/test/
    ['b', 'a', 'c']
    1.txt
    --------------------------------------------------------------------------------
    
    
    /data_1/everyday/0723/test/
    ['b', 'a', 'c']
    2.txt
    --------------------------------------------------------------------------------
    
    
    /data_1/everyday/0723/test/b
    ['h', 'f']
    7.txt
    --------------------------------------------------------------------------------
    
    
    /data_1/everyday/0723/test/b
    ['h', 'f']
    4.txt
    --------------------------------------------------------------------------------
    
    
    /data_1/everyday/0723/test/b/f
    []
    5.txt
    --------------------------------------------------------------------------------
    
    
    /data_1/everyday/0723/test/a
    ['e', 'd']
    3.txt
    --------------------------------------------------------------------------------
    
    
    /data_1/everyday/0723/test/c
    []
    5.txt
    --------------------------------------------------------------------------------
    

    可以用b目标来详细分析一下os.walk功能:

    ├── b
    │   ├── 4.txt
    │   ├── 7.txt
    │   ├── f
    │   │   └── 5.txt
    │   └── h
    
    /data_1/everyday/0723/test/b
    ['h', 'f']
    7.txt
    --------------------------------------------------------------------------------
    /data_1/everyday/0723/test/b
    ['h', 'f']
    4.txt
    --------------------------------------------------------------------------------
    /data_1/everyday/0723/test/b/f
    []
    5.txt
    --------------------------------------------------------------------------------
    

    可见:

    for root, dir, files in os.walk(root_dir):
    

    root是文件夹根目录,
    dir是root根目录下面的所有的文件夹,若没有文件夹就是空[]
    files是root根目录下面的所有文件
    列出所有的files,空文件夹不列出。
    所以:

    for root, dir, files in os.walk(path):
        for file in files:
            full_path = os.path.join(root, file)
    

    full_path = os.path.join(root, file)这句可以拿到目录下所有的文件!

    好记性不如烂键盘---点滴、积累、进步!
  • 相关阅读:
    python3.4+pyspider爬58同城(二)
    pyspider安装后,点击run,报pyhton has stop working或python已停止运行的错误
    PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\Users\video\AppData\Local\Temp\tmpfipzk8ma'--问题解决
    使用firefoxprofile,selenium设置firefox,初始化firefox
    排序算法讲解
    Java寫聊天小程序
    csproj项目工程文件的脚本/动态链接库设置
    常见的内存加密防破解及安全方案
    Animator直接引用FBX下的AnimClip与直接引用单独的AnimClip的对比
    Jupyter多内核的手动配置(Python多版本)
  • 原文地址:https://www.cnblogs.com/yanghailin/p/15048241.html
Copyright © 2011-2022 走看看