PDA

View Full Version : [SOLVED] Output of python to the shell



talsemgeest
November 26th, 2008, 09:57 AM
I am completely new to programming and have chosen python as my first language of choice. I have done a few tutorials and have a reasonable grip on the language (for a complete noob) and feel I am ready to write my first real program.

Just a quick question though, obvious and stupid: how to I get my python program to run commands? I'm hoping that from the input given, my program will generate a command and then run it as though I had typed it into the bash terminal.

So, hopefully this will be an easy question with a quick answer, thank you in advance for all help.

Tony Flury
November 26th, 2008, 10:02 AM
Look at the os.system builtin function - that will spawn a new shell and execute a command.

Note that this is a new shell - you will have to think about things like environment variables etc, and also any commands you execute wont appear in command history etc.

Greyed
November 26th, 2008, 10:02 AM
os.system() is the most simplistic way to do it but not the best.

Popen and its objects are more complex and the generally accepted manner to do it.

talsemgeest
November 26th, 2008, 10:17 AM
OK, can you give me some example code for the os.system() function? I tend to learn better that way.

Thanks!

Greyed
November 26th, 2008, 10:21 AM
import os
print("---------- os.listdir()")
for file in os.listdir('.'):
print file
print("---------- os.system('ls -1')")
os.system('ls -1')

talsemgeest
November 26th, 2008, 10:26 AM
Thank you both for your help, it is greatly appreciated. I think that should be all I need.