使用python的os.walk()对目标路径进行遍历

需求背景

在使用python处理和扫描系统文件的过程中,经常要使用到目录或者文件遍历的功能,这里通过引入os.walk()的功能直接来实现这个需求。

使用示例

由于功能模块本身比较简单,这里直接提供一个简单示例供参考:

# walker.py
import os

d = []
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/example2

This 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/

posted @ 2021-02-01 19:38  DECHIN  阅读(586)  评论(0编辑  收藏  举报