python3-os模块中的os.walk(目录树生成器)
#先看源码
def walk(top, topdown=True, onerror=None, followlinks=False):
"""Directory tree generator. For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of
the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name). If optional arg 'topdown' is true or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top down). If topdown is false, the triple
for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom up). When topdown is true, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse into the
subdirectories whose names remain in dirnames; this can be used to prune the
search, or to impose a specific order of visiting. Modifying dirnames when
topdown is false is ineffective, since the directories in dirnames have
already been generated by the time dirnames itself is generated. No matter
the value of topdown, the list of subdirectories is retrieved before the
tuples for the directory and its subdirectories are generated. By default errors from the os.scandir() call are ignored. If
optional arg 'onerror' is specified, it should be a function; it
will be called with one argument, an OSError instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object. By default, os.walk does not follow symbolic links to subdirectories on
systems that support them. In order to get this functionality, set the
optional argument 'followlinks' to true. Caution: if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk. walk never
changes the current directory, and assumes that the client doesn't
either.
原文注释大概这么些意思:
这是一个目录树的生成器。对于位于顶部的目录树中的每个目录产生一个3元元组。(dirpath,dirnames,filenames)
第一个为起始路径,第二个为起始路径下的所有文件夹的名字,第三个是起始路径下的所有非目录文件。
dirpath 是一个string,代表目录的路径,
dirnames 是一个list,包含了dirpath下所有子目录的名字。
filenames 是一个list,包含了非目录文件的名字。
注意:这些目录和文件名字不包含路径信息,如果需要得到全路径,需要使用os.path.join(dirpath, name).,通过for循环自动完成递归枚举
如果可选的参数 'topdown'为真或未指定,则(目录是自上而下生成的)。
如果topdown为false,则子目录(目录自下而上地生成)。
当topdown为true时,调用者可以就地修改dirnames列表,(例如,通过del或slice赋值),并且walk只会递归到
子目录,其名称保留在dirname中;这可以用来修剪搜索,或强制执行特定的访问命令。
修改目录名的时候topdown为false是无效的,因为在dirname中有目录已经生成的时间dirname本身是生成的。
不管topdown的值,子目录列表在生成目录及其子目录的元组。
参数onerror的默认值是"None",表示忽略文件遍历时产生的错误.如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历
总结:
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,则通过软链接访问目录。
python3-os模块中的os.walk(目录树生成器)的更多相关文章
- python的os模块中的os.walk()函数
os.walk('path')函数对于每个目录返回一个三元组,(dirpath, dirnames, filenames), 第一个是路径,第二个是路径下面的目录,第三个是路径下面的文件 如果加参数t ...
- python基础:os模块中关于文件/目录常用的函数使用方法
Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...
- os模块中关于文件/目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...
- os、os.path模块中关于文件、目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举 ...
- python编程os、os.path 模块中关于文件、目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名( ...
- python—— 文件的打开模式和文件对象方法 & os、os.path 模块中关于文件、目录常用的函数使用方法
引用自“鱼c工作室” 文件的打开模式和文件对象方法 : https://fishc.com.cn/forum.php?mod=viewthread&tid=45279&ext ...
- 有关文件夹与文件的查找,删除等功能 在 os 模块中实现
最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...
- OS模块中获取当前文件的绝对路径的相关方法
os.path.realpath(__file__) 作用:获取当前执行py脚本的绝对路径(在当前工作目录下的绝对路径) __file__ : 表示当前文件的本身,一般值是当前文件的相对路径 例如: ...
- Python OS模块中的fork方法实现多进程
import os '''使用OS模块中的fork方式实现多进程''' '''fork方法返回两次,分别在父进程和子进程中返回,子进程中永远返回0,父进程返回的是子进程的is''' if __name ...
随机推荐
- Kafka自我学习3-Scalable
1.After created the zookeeper cluster, we found all broker cluster topic can be find in zoo1, zoo2, ...
- iOS 之持久化存储 plist、NSUserDefaults、NSKeyedArchiver、数据库
1.什么是持久化? 本人找了好多文章都没有找到满意的答案,最后是从孙卫琴写的<精通Hibernate:Java对象持久化技术详解>中,看到如下的解释,感觉还是比较完整的.摘抄如下: 狭义的 ...
- MyBatis+Spring实现基本CRUD操作
一.MyBaits介绍 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的X ...
- HDU 2084 DP经典例子---数塔问题
http://acm.hdu.edu.cn/showproblem.php?pid=2084 #include "iostream" #include "cstdio&q ...
- SQLSERVER数据库置疑、可疑、脱机、单用户、紧急模式等的修复
数据库出现置疑.可疑.脱机.单用户.紧急模式主要是因为数据库的日志文件除了问题,2000和2008修复方式不一样,2008的修复脚本在2000中不适用,主要是不被2000识别. 假设数据库名为:eis ...
- NYOJ 349 Sorting It All Out (拓扑排序 )
题目链接 描述 An ascending sorted sequence of distinct values is one in which some form of a less-than ope ...
- 愚蠢的LCAAAAA~~~~(>_<)~~~~
很愤怒!特别愤怒!超级愤怒!!! 我LCA居然错了!!而且是那种特别愚蠢的错误 我把代码都交错了!!! silasila 话不多说,代码上特别详细了 #include<bits/stdc++.h ...
- linux中的阻塞机制及等待队列【转】
转自:http://www.cnblogs.com/gdk-0078/p/5172941.html 阻塞与非阻塞是设备访问的两种方式.驱动程序需要提供阻塞(等待队列,中断)和非阻塞方式(轮询,异步通知 ...
- pressmuSpiderr
#!/usr/bin/env python # encoding: utf-8 import requests from random import choice from lxml import h ...
- WPF中添加一个文本输入框,按Enter回车,执行绑定的Command
在WPF+WMMV模式中使用键盘和鼠标事件的绑定代码如下: <TextBox x:Name="SearchBox" Text="{Binding SearchTex ...