[英]How to use the items of the list, with the glob.glob to get the files?
[英]python glob.glob - how to find a specific file (or a list of files) without knowing how deep it in in subdirs?
现在,我使用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
更长的时间,即使是通过子进程。 此外,结果集是巨大的,我无法知道特定主机的架构是否具有相同的目录结构,所以我不知道在glob.glob()
输入多少个星号。
我的问题是,我怎样才能重复简单find | grep
的行为 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.