[Wolves] Another python question, running commands with variables as arguments.

Alex Willmer alex at moreati.org.uk
Wed Aug 17 19:12:45 UTC 2011


On 17 August 2011 12:56, Simon Burke <simonb at fatsportsman.eu> wrote:
> All I want to do is run a command, but arguments are stored in variables.
>
> This is where Im struggling.
>
> I have been using subprocess.Popen to run commands with little
> problem, but as soon as I want to get variables involved it all falls
> to pieces.
> My understanding is that I need to possibly use
> subprocess.Popen,communicate but I cant find a clear example or doc to
> detail how I am supposed to do this.
>
> Can someone help or point me in the right direction?

Simon,
It depends what you mean by variables and what you want to achieve.
Are you talking about Python variables (e.g. sys.argv), or environment
variables (i.e. $PWD, $HOME etc)?

You could use a variable as one of the arguments doing something like
the following:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> path = '/'
>>> pipe = subprocess.Popen(['/bin/ls', path], stdout=subprocess.PIPE)
>>> pipe.communicate()
('bin\nboot\ncdrom\ndev\netc\nhome\ninitrd.img\nlib\nlost+found\nmedia\nmnt\nopt\nproc\nroot\nsbin\nselinux\nsrv\nsys\ntmp\nusr\nvar\nvmlinuz\n',
None)

If you paste the code you have, and ask more specific question(s) we
should be able to help you much better. In general, vague questions
don't help us or you.

Regards, Alex
-- 
Alex Willmer <alex at moreati.org.uk>
http://twitter.com/moreati



More information about the Wolves mailing list