zoukankan      html  css  js  c++  java
  • Python 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

    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
    
    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
    
    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
    

      

      

      

  • 相关阅读:
    内存管理 Autorelease、retain、copy、assign的set方法和含义?
    解决 项目cocoapods diff: /../Podfile.lock: No such file or directory
    iOS按钮点击的频率 防止按钮连续点击时重复执行按钮的点击方法
    iOS 网络篇--PDF网络文件下载和打开
    使用runtime获取属性
    取到 tableview 自定义section header 上的button
    JavaScript第一天 改变DIV的样式
    iOS 因为reason: 'Pushing the same view controller instance more than once is not supported而奔溃(下)
    iOS 因为reason: 'Pushing the same view controller instance more than once is not supported而奔溃(上)
    iOS缓存功能
  • 原文地址:https://www.cnblogs.com/jsplyy/p/5638427.html
Copyright © 2011-2022 走看看