python os.walk 指定遍历深度_使用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/modul

ad8

e2.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的路径遍历功能的说明。

版权声明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值