gpt4 book ai didi

python glob.glob - 如何在不知道子目录中有多深的情况下查找特定文件(或文件列表)?

转载 作者:太空狗 更新时间:2023-10-29 11:05:15 33 4
gpt4 key购买 nike

现在,我使用 subprocess 来调用 find ,它可以很好地完成工作,但我追求的是 pythonic 的做事方式。

这是当前代码:

cmd = "find /sys/devices/pci* | grep '/net/' |grep address"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

在输出中我收到以下列表:

[root@host1 ~]# find /sys/devices/pci* |grep '/net/'|grep 'address'
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:00.0/0000:08:00.0/net/eth0/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:01.0/0000:09:00.0/net/eth1/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:02.0/0000:0a:00.0/net/rename4/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:03.0/0000:0b:00.0/net/eth3/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:04.0/0000:0c:00.0/net/eth4/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:05.0/0000:0d:00.0/net/eth5/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:06.0/0000:0e:00.0/net/eth6/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:07.0/0000:0f:00.0/net/eth7/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:08.0/0000:10:00.0/net/eth8/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:09.0/0000:11:00.0/net/eth9/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:0a.0/0000:12:00.0/net/eth10/address
/sys/devices/pci0000:00/0000:00:07.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/0000:07:0b.0/0000:13:00.0/net/eth11/address

现在,如果我执行 glob.glob('/sys/devices/pci*/*/*/*/*/*/*/net/'),我会得到一个列表目录,我什至可以查找文件,但它肯定比 find 花费的时间更长,即使是通过子进程。而且,结果集很大,我无法提前知 Prop 体主机的架构是否会有相同的目录结构,所以我不知道在 glob.glob()< 中输入多少星号.

我的问题是,如何重复简单的 find | 行为? grep 命令实现,或者,如果有更好的方法找到主机拥有的所有 NIC 的所有 MAC,无论是否处于事件状态(我在这里寻找特定的 MAC 模式)

编辑:不应该使用 glob,os.walk 似乎在做这项工作:

>>> for root, dirs, names in os.walk('/sys/devices/'):
... if 'address' in names and 'pci' in root:
... f = open(str(root + '/address'), 'r')
... mac = f.readlines()[0].strip()
... f.close()
... print mac
... eth = root.split('/')[-1]
... print eth

最佳答案

你检查过 os.walk() 了吗?

import os
for root, dirs, names in os.walk(path):
...

http://docs.python.org/library/os.html#os.walk

从上面的链接中,这里有一种跳过某些目录的方法:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print root, "consumes",
print sum(getsize(join(root, name)) for name in files),
print "bytes in", len(files), "non-directory files"
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories

关于python glob.glob - 如何在不知道子目录中有多深的情况下查找特定文件(或文件列表)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12733926/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com