PDA

View Full Version : embedded bash commands in python3?



earthpigg
December 9th, 2009, 11:56 PM
im a supernoob @ python and programming in general, so i dont know the right terms to use when google searching.

my bash-fu is decent, however, so it would behoove me to mix and match the two until my python-fu is up to speed.

lets take an imaginary python script...


python3 code
more code

run /path/to/bashscript.sh or (this) set of bash commands

back to python code
more code

is there a simple way to do this?

geirha
December 10th, 2009, 04:10 PM
>>> from subprocess import Popen, PIPE
>>> p = Popen("bash", stdout=PIPE, stdin=PIPE)
>>> p.stdin.write("echo 'hello'\n")
>>> p.stdin.close()
>>> p.stdout.read()
'hello\n'
>>> help("subprocess")


Though you are probably much better off doing everything in python.

falconindy
December 10th, 2009, 06:37 PM
Rather than starting a whole new shell, you can execute a single command:

p = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
p now contains the results of 'ls -l'. You can pipe it to another command as well:

p1 = Popen(['ls', '-l'], stdout=PIPE)
p2 = Popen(['grep', '^d'], stdin=p1.stdout, stdout=PIPE).communicate()[0]
p2 now contains the output of 'ls -l | grep ^d'. Notice that you're executing on the second command, not the first.

I wrote a bunch of scripts this way when I was first learning Python. I found it was good practice to go back and remove the multiple Popen executions by replacing them with native Python.

earthpigg
December 10th, 2009, 11:30 PM
great, thanks :D