How to get output of command called by os.system()?

P

Peng Yu

I need to integrate shell program with python. I'm wondering if there
is a way get the output of the shell program called by os.system().
Thank you!
 
R

Rominsky

I need to integrate shell program with python. I'm wondering if there
is a way get the output of the shell program called by os.system().
Thank you!

popen should do what your after. There are several modules that have
a popen method including os and subprocess. It will allow you to make
a system call similar to os.system, but it gives you pipe access, like
an open file, to the standard output and standard error if you use
subprocess.Popen. A simple example would be:

import subprocess
output = subprocess.Popen('pwd')
print('Present Working Directory is: ' + output.readline())

Hope that helps.

-John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top