Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:08:24+00:00 2026-06-12T11:08:24+00:00

Right now, I use subprocess to invocate find which does the job fine, but

  • 0

Right now, I use subprocess to invocate find which does the job fine, but I am after a pythonic way of doing things.

here’s the current code:

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

In the output I receive the following list:

[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

Now, if I do glob.glob('/sys/devices/pci*/*/*/*/*/*/*/net/') I do get a list of directories, and I can even look for the files, but it definitely seems to take longer than find does, even through subprocess. Moreover, the set of results is huge, and I can’t know ahead whether the specific hosts’ architecture will have the same directory structure, so I don’t know how many asterisks to enter in glob.glob().

My question is, how can I repeat the behaviour the simple find | grep command achieves, or, alternatively, if there is a nicer way of finding all the MACs of all the NICs a host has, whether active or not (I’m looking for specific MAC patterns here)

EDIT: Shouldn’t have used glob, os.walk seems to be doing the job:

>>> 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:08:26+00:00Added an answer on June 12, 2026 at 11:08 am

    Have you checked out os.walk()?

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

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

    From the above link, here is a way to skip over certain directories:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I use a ListBox but that allows the user to select an
Right now I use a StackPanel where I have a small image but I
Right now I use Sun-Studio, but Oracle is asking ridiculously high license fees for
Right now i use iText to generate a pdf automatically. And my problem is
Right now I can use this URL to request a Google Static Maps image
Right now I have been trying to use Launchpad's API to write a small
So right now I'm bashing my head - at the moment we use an
I have a problem right now with CodeIgniter : I use the REST Controller
I'm trying to use regular expression right now and I'm really confuse. I want
I'm learning MVVM right now. Since I've seen many tutorials or projects use only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.