How would I execute a command and have it's output returned as a list or string? For example (in Linux):

os.system("iwconfig")

prints a list of wireless devices to stdout. How can I make Python see those results as a list or string so I can search for certain devices (like wlan0 for example) and their associated info?

Recommended Answers

All 7 Replies

Use this code snippet. Write com = Command("iwconfig").run() .

Which module contains "Command"? I can't find it in os, sys or subprocess. I get an error that "name 'Command' is not defined".

AWESOME!! I see you wrote that too. Great job and thanks for the pointer in the right direction!

oh BTW just got summin to add.
I no ur linux and im win7 64-bit but hey...

i use:

import os

strng = os.system('dir') #using dir as example - a cmd function
print string #show us
os.system('pause') #give us time to reeeeeaaaaadddddd.

i no ive only done it as basic variable but u shud b able 2 play wif it.
i cant c y it shouldnt work on linux...

oh BTW just got summin to add.
I no ur linux and im win7 64-bit but hey...

i use:

import os

strng = os.system('dir') #using dir as example - a cmd function
print string #show us
os.system('pause') #give us time to reeeeeaaaaadddddd.

i no ive only done it as basic variable but u shud b able 2 play wif it.
i cant c y it shouldnt work on linux...

The return value of os.system depends on your OS, and the python documentation explicitly recommends using the subprocess module instead.

oh BTW just got summin to add.
I no ur linux and im win7 64-bit but hey...

i use:

import os

strng = os.system('dir') #using dir as example - a cmd function
print string #show us
os.system('pause') #give us time to reeeeeaaaaadddddd.

i no ive only done it as basic variable but u shud b able 2 play wif it.
i cant c y it shouldnt work on linux...

I tried that first. On Linux, os.system returns an integer (0 for True and oddly enough 246 for False). So it doesn't actually return the output, just the exit code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.