How do I pass a variable to os.popen?

J dreadpiratejeff at gmail.com
Mon Oct 31 16:46:09 EDT 2011


On Mon, Oct 31, 2011 at 16:16, extraspecialbitter
<pauldavidmena at gmail.com> wrote:

>    if line[10:20] == "Link encap":
>       interface=line[:9]
>    cmd = 'ethtool %interface'
>    print cmd
>    gp = os.popen(cmd)

because you're saying that cmd is 'ethtool %interface' as you pointed
out later on...

how about:

cmd = 'ethtool %s' % interface

note the spaces there... that tells it to convert the contents of
interface to a string and insert them into the string you're assigning
to cmd...

assuming interface is things like eth0, you shoud now see "ethtool
eth0" when the print statement runs.



More information about the Python-list mailing list