Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26247 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2012-07-30 13:58 +0200 |
| Last post | 2012-07-30 13:58 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Linux shell to python Peter Otten <__peter__@web.de> - 2012-07-30 13:58 +0200
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-07-30 13:58 +0200 |
| Subject | Re: Linux shell to python |
| Message-ID | <mailman.2726.1343649543.4697.python-list@python.org> |
Vikas Kumar Choudhary wrote:
> let me know if someone has tried to implement (grep and PIPE) shell
> commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2"
> | cut -c1-7'
>
> I tried to use python subprocess and OS.Popen modules.
subprocess is the way to go.
> I was trying porting from bash shell to python.
Here's an example showing how to translate a shell pipe:
http://docs.python.org/library/subprocess.html#replacing-shell-pipeline
But even if you can port the shell script literally I recommend a more
structured approach:
import subprocess
import itertools
def parse_data(lines):
for not_empty, group in itertools.groupby(lines, key=bool):
if not_empty:
triples = (line.partition(":") for line in group)
pairs = ((left, right.strip()) for left, sep, right in triples)
yield dict(pairs)
if __name__ == "__main__":
def get(field):
return entry.get(field, "").lower()
output = subprocess.Popen(["lspci", "-vmm"],
stdout=subprocess.PIPE).communicate()[0]
for entry in parse_data(output.splitlines()):
if "nvidia" in get("Vendor") and "usb" in get("Device"):
print entry["Slot"]
print entry["Device"]
print
Back to top | Article view | comp.lang.python
csiph-web