python3 os walk_Python3 os.walk()方法

语法

以下是 walk()方法的语法:

os.walk(top[, topdown=True[, οnerrοr=None[, followlinks=False]]])

参数

top --在目录每个目录根,得到3元组,即(dirpath, dirnames, filenames)

topdown -- 如果可选参数 topdown 的值是Ture或未指定,则目录从自上而下的扫描。 如果 topdown 设置为False,目录从自下而上的扫描。

onerror -- 这可能会显示错误继续遍历文件,或引发异常中止遍历。

followlinks -- 这可访问目录指向符号链接,如果设置为true。

返回值

此方法不返回任何值。

示例

下面的示例显示 walk()方法的使用。

# !/usr/bin/python3

import os

os.chdir("d:\\tmp")

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

让我们编译并运行上述程序,这将扫描所有的目录和子目录,结果如下所示:

.\python2\testdir\Readme_files\Lpt_Port_Config.gif

.\python2\testdir\Readme_files\ParallelPortViever.gif

.\python2\testdir\Readme_files\softcollection.css

.\python2\testdir\Readme_files\Thumbs.db

.\python2\testdir\Readme_files\Yellov_Ball.gif

.\python2\testdir\Readme.html

.\python2\testdir\Readme_files

.\python2\testdir

.\Applicationdocs.docx

.\book.zip

.\foo.txt

.\java.ppt

.\python2

如果你将 topdown 改变前的值为 True,那么它会得到以下结果:

.\Applicationdocs.docx

.\book.zip

.\foo.txt

.\java.ppt

.\python2

.\python2\testdir

.\python2\testdir\Readme.html

.\python2\testdir\Readme_files

.\python2\testdir\Readme_files\Lpt_Port_Config.gif

.\python2\testdir\Readme_files\ParallelPortViever.gif

.\python2\testdir\Readme_files\softcollection.css

.\python2\testdir\Readme_files\Thumbs.db

.\python2\testdir\Readme_files\Yellov_Ball.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python的os模块中的os.walk()函数是用来遍历文件夹的。它可以递归地遍历指定文件夹下的所有子文件夹和文件,并返回一个包含每个文件夹路径、子文件夹列表和文件列表的三元组的生成器。 而os.path.join()函数是用来连接路径的。它可以将多个路径组合成一个新的路径,并根据操作系统的不同自动添加正确的路径分隔符。这样可以方便地创建文件的绝对路径。 举个例子,如果我们有一个文件夹路径 `c:\Python`,并且文件夹中有一个名为 `a.txt` 的文件,我们可以使用os.path.join()函数将文件夹路径和文件名连接起来,生成完整的文件路径 `'c:\\Python\\a.txt'`。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python os.walk() 方法遍历文件目录](https://blog.csdn.net/weixin_34567079/article/details/114911951)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [如何用Python os.path.walk方法遍历搜索文件内容的操作详解_](https://blog.csdn.net/weixin_42216454/article/details/113963002)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值