all = os.walk(source_txt_path)# dirpath:从all中存储的source_txt_path下文件夹及子文件夹列表中取出每个文件夹及子文件夹路径
# dirnames :dirpath下的文件夹列表(不包括子文件夹)
# filenames :dirpath下的文件列表
for dirpath, dirnames, filenames in all:

topdown、onerror、followlinks参数貌似还不怎么用到,暂时不管。


doc:

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-tupledirpath, dirnames, filenamesdirpath is a string, the path to the directory.  dirnames is a list ofthe names of the subdirectories in dirpath (excluding '.' and '..').dirpath是一个字符串,是目录的路径。 dirnames是dirpath中子目录名称的列表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 indirpath, do os.path.join(dirpath, name).filenames是dirpath中非目录文件名称的列表。 请注意,列表中的名称只是名称,没有路径成分。 要获取目录路径中文件或目录的完整路径(从顶部开始),请执行os.path.join(dirpath,name)。If optional arg 'topdown' is true or not specified, the triple for adirectory is generated before the triples for any of its subdirectories(directories are generated top down).  If topdown is false, the triplefor a directory is generated after the triples for all of itssubdirectories (directories are generated bottom up).如果可选参数arg'topdown'为true或未指定,则在其任何子目录的三元组之前生成目录的三元组(目录是自上而下生成的)。 如果topdown为false,则在其所有子目录的三元组之后生成目录的三元组(目录自下而上生成)。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 thesubdirectories whose names remain in dirnames; this can be used to prune thesearch, or to impose a specific order of visiting.  Modifying dirnames whentopdown is false is ineffective, since the directories in dirnames havealready been generated by the time dirnames itself is generated. No matterthe value of topdown, the list of subdirectories is retrieved before thetuples for the directory and its subdirectories are generated.当topdown为true时,调用者可以就地修改目录名列表(例如,通过del或slice分配),而walk仅会递归到名称保留在目录名中的子目录中; 这可用于修剪搜索或强加特定的访问顺序。 当topdown为false时修改目录名无效,因为目录名本身生成时已经生成了目录名中的目录。 无论topdown的值如何,都将在生成目录及其子目录的元组之前检索子目录的列表。By default errors from the os.scandir() call are ignored.  Ifoptional arg 'onerror' is specified, it should be a function; itwill be called with one argument, an OSError instance.  It canreport the error to continue with the walk, or raise the exceptionto abort the walk.  Note that the filename is available as thefilename attribute of the exception object.默认情况下,将忽略os.scandir()调用中的错误。 如果指定了可选的arg'onerror',它应该是一个函数; 将使用一个参数OSError实例来调用它。 它可以报告错误以继续进行遍历,或者引发异常以中止遍历。 请注意,文件名可用作异常对象的文件名属性。By default, os.walk does not follow symbolic links to subdirectories onsystems that support them.  In order to get this functionality, set theoptional argument 'followlinks' to true.缺省情况下,os.walk在支持它们的系统上不跟随符号链接到子目录。 为了获得此功能,请将可选参数'followlinks'设置为true。Caution:  if you pass a relative pathname for top, don't change thecurrent working directory between resumptions of walk.  walk neverchanges the current directory, and assumes that the client doesn'teither.注意:如果您为top传递了相对路径名,请不要在恢复行走之间更改当前的工作目录。 walk永远不会更改当前目录,并假定客户端也不会更改当前目录。Example:import osfrom os.path import join, getsizefor root, dirs, files in os.walk('python/Lib/email'):print(root, "consumes", end="")print(sum([getsize(join(root, name)) for name in files]), end="")print("bytes in", len(files), "non-directory files")if 'CVS' in dirs:dirs.remove('CVS')  # don't visit CVS directories"""top = fspath(top)dirs = []nondirs = []walk_dirs = []# We may not have read permission for top, in which case we can't# get a list of the files the directory contains.  os.walk# always suppressed the exception then, rather than blow up for a# minor reason when (say) a thousand readable directories are still# left to visit.  That logic is copied here.# 我们可能没有top的读取权限,在这种情况下,我们无法获取目录包含的文件的列表。 # 那时os.walk总是抑制该异常,而不是因为(尽管如此)当仍然有千个可读目录需要访问时才因次要原因而崩溃。 # 该逻辑复制到此处。try:# Note that scandir is global in this module due# to earlier import-*.# 注意,由于较早的import-*,scandir在此模块中是全局的。scandir_it = scandir(top)except OSError as error:if onerror is not None:onerror(error)returnwith scandir_it:while True:try:try:entry = next(scandir_it)except StopIteration:breakexcept OSError as error:if onerror is not None:onerror(error)returntry:is_dir = entry.is_dir()except OSError:# If is_dir() raises an OSError, consider that the entry is not# a directory, same behaviour than os.path.isdir().# 如果is_dir()引发OSError,请考虑该条目不是目录,其行为与os.path.isdir()相同。is_dir = Falseif is_dir:dirs.append(entry.name)else:nondirs.append(entry.name)if not topdown and is_dir:# Bottom-up: recurse into sub-directory, but exclude symlinks to# directories if followlinks is False# 自下而上:递归到子目录,但如果followlinks为False,则排除指向目录的符号链接if followlinks:walk_into = Trueelse:try:is_symlink = entry.is_symlink()except OSError:# If is_symlink() raises an OSError, consider that the# entry is not a symbolic link, same behaviour than# os.path.islink().# 如果is_symlink()引发OSError,请考虑该条目不是符号链接,其行为与os.path.islink()相同。is_symlink = Falsewalk_into = not is_symlinkif walk_into:walk_dirs.append(entry.path)# Yield before recursion if going top down# 如果自上而下,则在递归前生成if topdown:yield top, dirs, nondirs# Recurse into sub-directories# 递归到子目录islink, join = path.islink, path.joinfor dirname in dirs:new_path = join(top, dirname)# Issue #23605: os.path.islink() is used instead of caching# entry.is_symlink() result during the loop on os.scandir() because# the caller can replace the directory entry during the "yield"# above.# 问题#23605:在os.scandir()上的循环期间,使用了os.path.islink()而不是缓存entry.is_symlink()结果,因为调用者可以在上面的“ yield”期间替换目录条目。if followlinks or not islink(new_path):yield from walk(new_path, topdown, onerror, followlinks)else:# Recurse into sub-directories# 递归到子目录for new_path in walk_dirs:yield from walk(new_path, topdown, onerror, followlinks)# Yield after recursion if going bottom up# 如果自下而上,则递归后生成yield top, dirs, nondirs

参考文章:Python3 os.walk() 方法

python 文件操作 os.walk() 方法相关推荐

  1. python 文件操作 os.read() 方法 读文本文件 读取文字

    引用文章:Python3 os.read() 方法

  2. Python基础教程 os.walk()方法

    os.walk方法,主要用来遍历一个目录内各个子目录和子文件. os.walk(top, topdown=True, onerror=None, followlinks=False) 可以得到一个三元 ...

  3. python 文件操作 os.mkdir()函数

    os.mkdir() 方法用于以数字权限模式创建目录.默认的模式为 0777 (八进制). os.mkdir(path[, mode]) path – 要创建的目录 mode – 要为目录设置的权限数 ...

  4. python 文件操作 os.listdir() 遍历文件

    功能: 返回所给路径中所有文件(包含文件夹)的名称列表 doc: def listdir(*args, **kwargs): # real signature unknown""& ...

  5. python 文件操作 os模块和shutil模块

    转载自:http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html ############################## ...

  6. python 文件操作 os 如何检索文件夹内文件数量

    参考文章1:python os获取文件夹中文件夹内文件的数量,保存为CSV文件 参考文章2:用python计算大文件夹下小文件夹里文件的个数

  7. python 文件操作 os.readlines()函数用法

    引用文章:Python File readlines() 方法

  8. python 文件操作 os.readline()函数用法

    引用文章:Python File readline() 方法

  9. python 文件操作 os.path.join(path, *paths) 路径合成(追加)

    os.path.join(path1[, path2[, ...]]) 把目录和文件名合成一个路径 参考文章:Python3 os.path() 模块

最新文章

  1. 二叉树节点数据结构-练习 5 二叉树的建立 遍历
  2. 【VMCloud云平台】Demo应用搭建(二)
  3. while 循环判断时 遇到赋值表达式
  4. 1200万!硅谷AI大牛一年赚够北京二环一套房
  5. 烂泥:LVM学习之LVM基础
  6. 如何判断脸型测试软件,【图】脸型判断 教你非常准确的测试方法_脸型_伊秀美容网|yxlady.com...
  7. Bootstrap Paginator 分页插件参数介绍及使用
  8. 每日一句20191019
  9. Keil C51软件的使用
  10. c#json对象转数组_如何将Json数组转换为C#中的对象列表
  11. C语言 数组指针详解
  12. hybird app
  13. Python实现简单自动升级exe程序版本并自动运行
  14. C# 计算农历日期方法
  15. 学习Java. 基础 17: 二维数组
  16. 59.bouncing results
  17. Fractional Fourier Image Transformer forMultimodal Remote Sensing Data Classification
  18. SSL证书的几个误解,正确认识SSL证书
  19. 新能源汽车应该何去何从?
  20. Hadoop2.7下载安装

热门文章

  1. 【SD】交货单如何在保存时更改LIKP表的值?
  2. SE43自定义sap菜单
  3. 可手工拖拽alv行记录的实例
  4. 8、Power Map—实例:制作独立播放的视频
  5. 华为SAP解决方案为海澜之家带来新的科技创新
  6. SAP反记帐和红字冲销
  7. webdynpro GOS BDS 文档/附件 上传下载处理
  8. 月活675万 三翼鸟以三大能力“重建”行业赛道
  9. git公有转私有_【IT新手之路】客户端组件化之私有库搭建
  10. php函数计算加法,JavaScript_javascript实现一个数值加法函数,废话不多说,直接奉上代码 JS - phpStudy...