zoukankan      html  css  js  c++  java
  • Python 之 os.walk()

    原文地址https://www.cnblogs.com/JetpropelledSnake/p/8982495.html          http://www.runoob.com/python/os-walk.html

    os.walk方法,主要用来遍历一个目录内各个子目录和子文件。

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

    可以得到一个三元tupple(dirpath, dirnames, filenames), 

    第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件。

    dirpath 是一个string,代表目录的路径,

    dirnames 是一个list,包含了dirpath下所有子目录的名字。

    filenames 是一个list,包含了非目录文件的名字。

    这些名字不包含路径信息,如果需要得到全路径,需要使用os.path.join(dirpath, name).

    通过for循环自动完成递归枚举

    例如:

    F:aaa 目录是这样的文件目录结构

    F:aaa

    |--------1.txt

    |--------2.txt

    |--------3.txt

    |--------4

             |-------5.txt

             |-------6.txt

             |-------7.txt

    分别打印各个参数的实际值

    复制代码
    #!/usr/bin/env python  
    # 2.py  
    # use UTF-8  
    # Python 3.3.0  
      
    # os.walk()的使用  
    import os  
      
    # 枚举dirPath目录下的所有文件  
      
    def main():  
    #begin  
        fileDir = "F:" + os.sep + "aaa"     # 查找F:aaa 目录下    
        for root, dirs, files in os.walk(fileDir):  
        #begin  
            print(root)  
            print(dirs)  
            print(files)  
        #end  
        os.system("pause")  
    #end  
      
    if __name__ == '__main__':  
    #begin  
        main()  
    #end  
      
      
    # 输出  
    # F:aaa  
    # ['4']  
    # ['1.txt', '2.txt', '3.txt']  
    # F:aaa4  
    # []  
    # ['5.txt', '6.txt', '7.txt']  
    复制代码

    你也可以这样,用元组a,通过for循环即可完成目录的递归.

    复制代码
    #!/usr/bin/env python  
    # 3.py  
    # use UTF-8  
    # Python 3.3.0  
      
    # os.walk()的使用  
    import os  
      
    # 枚举dirPath目录下的所有文件  
      
    def main():  
    #begin  
        fileDir = "F:" + os.sep + "aaa"     # 查找F:aaa 目录下    
        for a in os.walk(fileDir):  
        #begin  
            print(a[0])  
            print(a[1])  
            print(a[2])  
        #end  
        os.system("pause")  
    #end  
      
    if __name__ == '__main__':  
    #begin  
        main()  
    #end  
      
      
    # 输出  
    # F:aaa  
    # ['4']  
    # ['1.txt', '2.txt', '3.txt']  
    # F:aaa4  
    # []  
    # ['5.txt', '6.txt', '7.txt']  
    复制代码

    你还可以这样,先打印目录,再打印各个文件

    复制代码
    #!/usr/bin/env python  
    # 2.py  
    # use UTF-8  
    # Python 3.3.0  
      
    # os.walk()的使用  
    import os  
      
    # 枚举dirPath目录下的所有文件  
      
    def main():  
    #begin  
        fileDir = "F:" + os.sep + "aaa"     # 查找F:aaa 目录下    
        for root, dirs, files in os.walk(fileDir):  
        #begin  
            for dir in dirs:  
            #begin  
                print(os.path.join(root, dir))  
            #end  
            for file in files:  
            #begin  
                print(os.path.join(root, file))  
            #end  
        #end  
        os.system("pause")  
    #end  
      
    if __name__ == '__main__':  
    #begin  
        main()  
    #end  
      
      
    # 输出  
    # F:aaa4  
    # F:aaa1.txt  
    # F:aaa2.txt  
    # F:aaa3.txt  
    # F:aaa45.txt  
    # F:aaa46.txt  
    # F:aaa47.txt  
    复制代码

    Python os.walk() 方法

    os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。

    os.walk() 方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。

    在Unix,Windows中有效。

    语法

    walk()方法语法格式如下:

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

    参数

    • top -- 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。

      • root 所指的是当前正在遍历的这个文件夹的本身的地址
      • dirs 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
      • files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
    • topdown --可选,为 True,则优先遍历 top 目录,否则优先遍历 top 的子目录(默认为开启)。如果 topdown 参数为 True,walk 会遍历top文件夹,与top 文件夹中每一个子目录。

    • onerror -- 可选, 需要一个 callable 对象,当 walk 需要异常时,会调用。

    • followlinks -- 可选, 如果为 True,则会遍历目录下的快捷方式(linux 下是 symbolic link)实际所指的目录(默认关闭)。

    返回值

    该方法没有返回值。

    实例

    以下实例演示了 walk() 方法的使用:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import os
    for root, dirs, files in os.walk(".", topdown=False):
        for name in files:
            print(os.path.join(root, name))
        for name in dirs:
            print(os.path.join(root, name))

    执行以上程序输出结果为:

    ./.bash_logout
    ./amrood.tar.gz
    ./.emacs
    ./httpd.conf
    ./www.tar.gz
    ./mysql.tar.gz
    ./test.py
    ./.bashrc
    ./.bash_history
    ./.bash_profile
    ./tmp
    ./tmp/test.py
  • 相关阅读:
    CentOS虚拟机查询jdk路径
    jsp定义全局变量:读取properties文件
    mysql查看视图用户
    httpclient 方式提供接口
    maven打包添加依赖
    mysql死锁+解决
    mysql 安装-zip版
    input type="hidden" js获取不到值(document.getelementbyid OR $(#).val())
    Mybatis——choose, when, otherwise可以达到switch case效果
    《Linux就该这么学》第九天课程
  • 原文地址:https://www.cnblogs.com/111testing/p/9574804.html
Copyright © 2011-2022 走看看