• python学习之os.walk()


    os.walk(top,topdown = True,onerror = None,followlinks = False)

    参数

    • top -- 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames, filenames)【文件夹路径, 文件夹名字, 文件名】。

    • topdown --可选,为True或者没有指定, 一个目录的的3-元组将比它的任何子文件夹的3-元组先产生 (目录自上而下)。如果topdown为 False, 一个目录的3-元组将比它的任何子文件夹的3-元组后产生 (目录自下而上)。

    • onerror -- 可选,是一个函数; 它调用时有一个参数, 一个OSError实例。报告这错误后,继续walk,或者抛出exception终止walk。

    • followlinks -- 设置为 true,则通过软链接访问目录。

    返回值

    该方法没有返回值。

    实例

    # -*- coding: utf-8 -*-
    #
    import os
    import os.path
    for root, dirs, files in os.walk("D:Leocew", topdown=False):
        for name in files:
            print(name)
            print('-'*10)
        for name in dirs:
            print(name)
            print('#'*10)
    

     执行结果

    2.txt
    ----------
    ew
    ##########
    新建文本文档.txt
    ----------
    1
    ##########
    2
    ##########

    使用tree命令查询

    tree D:Leocew
    D:LEOCEW
    ├─1
    └─2
        └─ew
  • 相关阅读:
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
    第 6 章 Cinder
  • 原文地址:https://www.cnblogs.com/leomei91/p/7651982.html
Copyright © 2020-2023  润新知