Reputation: 1379
I'm trying to capture the output of a bash line I'm executing in my python script using the following code:
call_bash = subprocess.Popen(['/space/jazz/1/users/gwarner/test.sh'],stdout=subprocess.PIPE)
output = call_bash.communicate()[0]
This works fine but I'd like to change it so that that bash code is written directly into this python script (eliminating the need for a separate .sh file). I know that I can run bash code from python with subprocess.call()
but I can't figure out how to capture the output (what normally gets printed to the terminal incase I'm using the wrong terminology). Can anyone help?
I'm running python 2.7 on CentOS
Upvotes: 0
Views: 1579
Reputation: 14520
The first argument to subprocess.Popen is a list of arguments (unless you specify shell=True
).
So you could do your example like
call_bash = subprocess.Popen(['ssh', '[email protected]', 'scp me#comp:/~/blah.text ep#comp:/register/'], stdout=subprocess.PIPE)
output = call_bash.communicate()[0]
This will invoke the ssh
command with 2 arguments, [email protected]
and scp me#comp:/~/blah.text ep#comp:/register/
.
Upvotes: 2