PDA

View Full Version : [ubuntu] python output for shell command?



Unewbeginner
November 29th, 2010, 04:25 PM
How could I make the output of a python program as a command? For example, my python program print:
cd /home/chris/Books/2010 and then Ubuntu shell execute the command above. Thanks!!

sisco311
November 29th, 2010, 04:32 PM
$(command)

See:

man bash | less +/"Command Substitution"

Unewbeginner
November 30th, 2010, 12:29 AM
Sorry, when I add the code below in my python program, it doesn't work as I thought:

print "$ cd /home/chirs/book/2010"

anything I did wrong?

Unewbeginner
November 30th, 2010, 12:30 AM
Sorry, when I add the code below in my python program, it doesn't work as I thought:


print "$ cd /home/chirs/book/2010"

anything I did wrong?

Unewbeginner
December 1st, 2010, 12:16 PM
help please ......

sisco311
December 1st, 2010, 12:41 PM
Oh, you want something like:


import os

cmd='ls -al /home'
os.system(cmd)


http://docs.python.org/tutorial/stdlib.html#operating-system-interface

AngusH
December 1st, 2010, 02:43 PM
Sorry, when I add the code below in my python program, it doesn't work as I thought:


print "$ cd /home/chirs/book/2010"

anything I did wrong?

Listen to sisco311, that's what you want, but also you don't want to be using that $ symbol. The shell shows it as an indicator of your privelidge level and the kind of input it wants, it's not a part of the command.
Angus