Python os.walk() 方法
来源:互联网 发布:汕头淘宝村拿货 编辑:程序博客网 时间:2024/06/06 09:34
Description
The method walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up.
Syntax
Following is the syntax for walk() method:
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
Parameters
top -- Each directory rooted at directory, yields 3-tuples, i.e., (dirpath, dirnames, filenames)
topdown -- If optional argument topdown is True or not specified, directories are scanned from top-down. If topdown is set to False, directories are scanned from bottom-up.
onerror -- This can show error to continue with the walk, or raise the exception to abort the walk.
followlinks -- This visits directories pointed to by symlinks, if set to true.
Return Value
This method does not return any value.
Example
The following example shows the usage of walk() method.
# !/usr/bin/pythonimport osfor 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))
输出:
Let us compile and run the above program, this will scan all the directories and subdirectories bottom-to-up
./tmp/test.py./.bash_logout./amrood.tar.gz./.emacs./httpd.conf./www.tar.gz./mysql.tar.gz./test.py./.bashrc./.bash_history./.bash_profile./tmp
If you will change the value of topdown to True, then it will give you the following result:
./.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
- Python os.walk() 方法
- Python的os.walk()方法详细讲解
- [python] os.walk()与os.path.walk()
- python os.walk和os.path.walk
- Python os.walk() 和 os.path.walk()
- python os.walk和os.path.walk
- Python os.walk()和os.path.walk()
- python os.walk
- python os walk笔记
- Python os.walk
- Python os.walk()
- Python os.walk() 简介
- Python os.walk 函数
- Python 之os.walk()
- Python os.walk()
- Python os.walk
- os.walk()方法
- 使用os.walk()方法
- C++调用本地js
- 电脑关机后自动重启开机,或过一段时间自动重启开机问题解决
- MongDB入门资料(整理)
- VMWare10下克隆centos6
- 内存管理
- Python os.walk() 方法
- HTTP返回码中301与302的区别
- 从零开始,教你用Webpack构建React基础工程
- Xcode快捷键收集
- ios 补充占位符
- 杭电oj编码2020
- 移出字符串中分号前后的空格,正则
- 软件基本知识整理
- uva 11181 Probability|Given 条件概率