Tags: butam, ossystem, output, popen, programming, python, redirect, system, trouble, variable

Redirect os.system output

On Programmer » Python

6,661 words with 9 Comments; publish: Tue, 06 May 2008 21:10:00 GMT; (200139.65, « »)

I would like to redirect the output from os.system to a variable, but

am having trouble. I tried using os.popen(..).read() ...but that

doesn't give me exactly what i want.

..this is windows by the way.

For example:

tmp = os.popen("hostname").read()

...works as expected.

however,

tmp = os.popen("cmd").read()

...i would like to have access to the cmd process...i.e. enter commands

like a normal command line. os.system() allows this, but i dont want

output to the screen..i wanna store it to a variable. then send

content of variable elsewhere, receive more input and submit it.

almost emulate the windows command prompt.

any ideas?

All Comments

Leave a comment...

  • 9 Comments
    • maybe you should look at subprocess module

      I have one expamle, this is Linux though

      226

      hth, Daniel

      #1; Tue, 06 May 2008 21:12:00 GMT
    • jas wrote:

      > I would like to redirect the output from os.system to a variable, but

      > am having trouble. I tried using os.popen(..).read() ...but that

      > doesn't give me exactly what i want.

      Here is an example using subprocess:

      http://groups.google.com/group/comp...87e8e2a3?hl=en&

      Kent

      > ..this is windows by the way.

      > For example:

      > tmp = os.popen("hostname").read()

      > ...works as expected.

      > however,

      > tmp = os.popen("cmd").read()

      > ...i would like to have access to the cmd process...i.e. enter commands

      > like a normal command line. os.system() allows this, but i dont want

      > output to the screen..i wanna store it to a variable. then send

      > content of variable elsewhere, receive more input and submit it.

      > almost emulate the windows command prompt.

      > any ideas?

      >

      #2; Tue, 06 May 2008 21:13:00 GMT
    • Any other ideas? or examples of using subprocess to do what I was

      asking?

      Kent Johnson wrote:

      > jas wrote:

      > Here is an example using subprocess:

      > [url]http://groups.google.com/group/comp.lang.python/msg/9fa3a3c287e8e2a3?hl=en&[/url

      ]

      > Kent

      >

      #3; Tue, 06 May 2008 21:14:00 GMT
    • jas wrote:

      > Any other ideas? or examples of using subprocess to do what I was

      > asking?

      Actually I thought I was giving an example of what you were asking:

      - on windows

      - send a series of commands to a command process

      - capture the result to a variable

      The example I referenced sends a series of HELP commands to cmd.exe, capture

      s the output of the commands and saves it to a file.

      What did I miss?

      Kent

      >

      > Kent Johnson wrote:

      >

      >

      #4; Tue, 06 May 2008 21:15:00 GMT
    • I see that, although I don't totall grasp the code. However, I am

      looking to basically emulate a command prompt. i.e. everything u see

      in the windows command prompt should be displayed back in python.

      How can I do it without files?

      Kent Johnson wrote:

      > jas wrote:

      > Actually I thought I was giving an example of what you were asking:

      > - on windows

      > - send a series of commands to a command process

      > - capture the result to a variable

      > The example I referenced sends a series of HELP commands to cmd.exe, captu

      res the output of the commands and saves it to a file.

      > What did I miss?

      > Kent

      >

      #5; Tue, 06 May 2008 21:16:00 GMT
    • Ok, I tried this...

      C:\>python

      Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]

      on win32

      Type "help", "copyright", "credits" or "license" for more information.

      'result' is not recognized as an internal or external command,

      operable program or batch file.

      basically I was opening to send the "ipconfig" command to cmd.exe and

      store the result in the "result" variable. But you can see there was

      an error with result.

      Ideas?

      jas wrote:

      > I see that, although I don't totall grasp the code. However, I am

      > looking to basically emulate a command prompt. i.e. everything u see

      > in the windows command prompt should be displayed back in python.

      > How can I do it without files?

      > Kent Johnson wrote:

      #6; Tue, 06 May 2008 21:17:00 GMT
    • doesn't sound to encouraging :)

      How about something with os.popen?

      in = os.popen("cmd", "w")

      in.write("hostname")

      I tried this, and I get "IOError: [Errno 22] Invalid Argument"

      I am not sure why this isnt working.

      Steve Holden wrote:

      > jas wrote:

      > It looks to me like the line you thought you were typing at the Python

      > command interpreter actually got snagged by the command processor you

      > just ran, which presumably is taking its input from the console just

      > like Python is.

      >

      > Haven't used subprocess much yet, but I will just mention that this kind

      > of thing always looks easy in principle and turns out to be surprisingly

      > gnarly and difficult in practice.

      > I'm not suggesting you shouldn't continue, but you are going to learn a

      > *lot* as you proceed. Good luck.

      > regards

      > Steve

      > [...]

      > --

      > Steve Holden +44 150 684 7255 +1 800 494 3119

      > Holden Web LLC www.holdenweb.com

      > PyCon TX 2006 www.python.org/pycon/

      #7; Tue, 06 May 2008 21:18:00 GMT
    • jas wrote:

      > Ok, I tried this...

      > C:\>python

      > Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]

      > on win32

      > Type "help", "copyright", "credits" or "license" for more information.

      >

      > 'result' is not recognized as an internal or external command,

      > operable program or batch file.

      >

      > basically I was opening to send the "ipconfig" command to cmd.exe and

      > store the result in the "result" variable. But you can see there was

      > an error with result.

      This works for me:

      import subprocess as sp

      p = sp.Popen("ipconfig", stdout=sp.PIPE)

      result = p.communicate()[0]

      print result

      Kent

      #8; Tue, 06 May 2008 21:19:00 GMT
    • Kent,

      Yes, your example does work. So did os.popen...however, the problem

      is specific to "cmd.exe".

      Have you tried that yet?

      Thanks!

      Kent Johnson wrote:

      > jas wrote:

      > This works for me:

      > import subprocess as sp

      > p = sp.Popen("ipconfig", stdout=sp.PIPE)

      > result = p.communicate()[0]

      > print result

      > Kent

      #9; Tue, 06 May 2008 21:20:00 GMT