python os.walk是怎样的呢?下面就让我们一起来了解一下吧:
os.walk是属于python下的一个方法,os.walk()方法一般是用来通过在目录树中游走输出在目录中的文件名,向上或是向下。简单来说,os.walk()方法是属于一个简单易用的文件、目录遍历器,能够更好地帮助我们高效的处理各种文件、目录等方面的事务。
语法格式:
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
参数说明:
top -- 是所要遍历的目录的地址,返回的一般是一个三元组(即root,dirs,files)。
1.root所指的一般是当前正在遍历的这个文件夹的本身的地址。
2.dirs是属于一个list,其内容是该文件夹中所有的目录的名字(需要注意的是并不包括子目录)。
3.files也是list,其中的内容是该文件夹中所有的文件(但并不包括子目录)。
topdown --可选,为True,那么会优先遍历top目录,若不是则会优先遍历top的子目录(通常是默认为开启的)。若是topdown参数为True,那么walk就会遍历top文件夹,与top文件夹中每一个子目录。
onerror -- 可选,需要一个callable对象,当walk需要异常时,就会调用。
followlinks -- 可选,若是为True,那么就会遍历目录下的快捷方式(linux下是软连接symbolic link )实际所指的目录(默认为关闭),若是为False,那么就会优先遍历 top的子目录。
参考范例:
输入代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
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))
输出结果:
./.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
以上就是小编的分享了,希望能够帮助到大家。
Python3使用os.walk()对目标路径进行遍历
目录
在使用python处理和扫描系统文件的过程中,经常要使用到目录或者文件遍历的功能,这里通过引入os.walk()的功能直接来实现这个需求。
使用示例由于功能模块本身比较简单,这里直接提供一个简单示例供参考:
# walker.pyimport osd = []f = []for root, dirs, files in os.walk('/home/dechin/projects/2021-python/'): for dir in dirs: d.append(os.path.join(root, dir)) for file in files: f.append(os.path.join(root, file))print ('Thie is the directories list:')for dir in d: print (dir)print ('\nThis is the files list:')for file in f: print (file)
在这个示例中,我们对本机目录/home/dechin/projects/2021-python/下的文件进行检索和遍历,最后将绝对路径保存到两个数列中分别进行保存。注意在os.walk()执行的过程中,是不对文件夹和文件进行区分的,因此中间遍历的顺序是无法控制的。关于文件夹和文件的无差别处理,跟系统中存储文件夹/文件的编号形式(innode)有关。在前面写的这一篇博客中有介绍Linux系统下对指定目录的innode等特性的配置和处理,读者可以自行参考。
这个os.walk()的示例执行结果如下:
[dechin@dechin-manjaro path_walk]$ python3 walker.py Thie is the directories list:/home/dechin/projects/2021-python/line_profiler/home/dechin/projects/2021-python/progressbar/home/dechin/projects/2021-python/bandit_test/home/dechin/projects/2021-python/path_walk/home/dechin/projects/2021-python/os_security/home/dechin/projects/2021-python/excute/home/dechin/projects/2021-python/pycuda/home/dechin/projects/2021-python/decorator/home/dechin/projects/2021-python/tmp_file/home/dechin/projects/2021-python/bandit_test/level2/home/dechin/projects/2021-python/excute/__pycache__/home/dechin/projects/2021-python/decorator/2/home/dechin/projects/2021-python/decorator/1/home/dechin/projects/2021-python/decorator/1/example1/home/dechin/projects/2021-python/decorator/1/example2This is the files list:/home/dechin/projects/2021-python/line_profiler/fmath.f90/home/dechin/projects/2021-python/line_profiler/sin_profiler_test.py.lprof/home/dechin/projects/2021-python/line_profiler/fmath.cpython-38-x86_64-linux-gnu.so/home/dechin/projects/2021-python/line_profiler/line_profiler_test.py.lprof/home/dechin/projects/2021-python/line_profiler/line_profiler_test.py/home/dechin/projects/2021-python/line_profiler/sin_profiler_test.py/home/dechin/projects/2021-python/progressbar/test_rich.py/home/dechin/projects/2021-python/progressbar/test_tqdm.py/home/dechin/projects/2021-python/bandit_test/test_bandit_power.py/home/dechin/projects/2021-python/bandit_test/test_bandit.html/home/dechin/projects/2021-python/bandit_test/.bandit/home/dechin/projects/2021-python/bandit_test/subprocess_Popen.py/home/dechin/projects/2021-python/bandit_test/test_power.html/home/dechin/projects/2021-python/bandit_test/test_bandit.txt/home/dechin/projects/2021-python/bandit_test/bad.py/home/dechin/projects/2021-python/bandit_test/gen.py/home/dechin/projects/2021-python/bandit_test/bad.txt/home/dechin/projects/2021-python/bandit_test/level2/test_random.py/home/dechin/projects/2021-python/path_walk/walker.py/home/dechin/projects/2021-python/os_security/file-test.py/home/dechin/projects/2021-python/os_security/fdopen-test.py/home/dechin/projects/2021-python/os_security/test1.txt/home/dechin/projects/2021-python/os_security/test2.txt/home/dechin/projects/2021-python/os_security/test5.txt/home/dechin/projects/2021-python/os_security/test3.txt/home/dechin/projects/2021-python/os_security/test4.txt/home/dechin/projects/2021-python/excute/module2.py/home/dechin/projects/2021-python/excute/module1.py/home/dechin/projects/2021-python/excute/__pycache__/module2.py/home/dechin/projects/2021-python/excute/__pycache__/module1.py/home/dechin/projects/2021-python/excute/__pycache__/module1.cpython-38.pyc/home/dechin/projects/2021-python/excute/__pycache__/module1.pyc/home/dechin/projects/2021-python/excute/__pycache__/module1.cpython-38.opt-1.pyc/home/dechin/projects/2021-python/pycuda/test_pycuda.py/home/dechin/projects/2021-python/decorator/requirements.py/home/dechin/projects/2021-python/decorator/decorator.py/home/dechin/projects/2021-python/decorator/test_decorator.py/home/dechin/projects/2021-python/decorator/1/example1/rprint/home/dechin/projects/2021-python/decorator/1/example2/rprint/home/dechin/projects/2021-python/tmp_file/tempfile_test.py
到这里功能演示就结束了,使用os.walk()唯一需要注意的一点就是,在Windows系统和Linux系统下的使用有所区别,在这一篇博客中有对windows系统下使用python的路径遍历功能的说明。
版权声明本文首发链接为:https://www.cnblogs.com/dechinphy/p/walker.html
作者ID:DechinPhy
更多原著文章请参考:https://www.cnblogs.com/dechinphy/