Redirect os.system output

J

jas

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?
 
?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

maybe you should look at subprocess module

I have one expamle, this is Linux though
>>> import subprocess as sp
>>> p1 = sp.Popen(["ls", "-l"], stdout = sp.PIPE)
>>> p2 = sp.Popen(["wc", "-c"], stdin = p1.stdout, stdout = sp.PIPE)
>>> print p2.stdout.read()
226

hth, Daniel
 
K

Kent Johnson

jas said:
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, captures the output of the commands and saves it to a file.

What did I miss?

Kent
 
J

jas

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?
 
J

jas

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?
 
S

Steve Holden

jas said:
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.
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
[...]
 
J

jas

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 said:
jas said:
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.
import subprocess as sp
p = sp.Popen("cmd", stdout=sp.PIPE)

result = p.communicate("ipconfig")

'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.
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
[...]
 
K

Kent Johnson

jas said:
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
 
J

jas

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 said:
jas said:
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.
import subprocess as sp
p = sp.Popen("cmd", stdout=sp.PIPE)

result = p.communicate("ipconfig")

'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
 
P

Paul Dale

You might want to try python expect which gives you a very simple and
scriptable interface to a process.

http://pexpect.sourceforge.net/

I've been using it on windows to automate a few things.

Cheers,

Paul
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:

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.



import subprocess as sp
p = sp.Popen("cmd", stdout=sp.PIPE)

result = p.communicate("ipconfig")


'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
 
J

jas

Paul,
I did ceck out the PExpect, however, I thought it was not ported for
Windows. Did you find a ported version? If not, what did you have to
do to be able to use it?

Thanks

Paul said:
You might want to try python expect which gives you a very simple and
scriptable interface to a process.

http://pexpect.sourceforge.net/

I've been using it on windows to automate a few things.

Cheers,

Paul
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:


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.



import subprocess as sp
p = sp.Popen("cmd", stdout=sp.PIPE)

result = p.communicate("ipconfig")


'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
 
P

Paul Dale

pexpect is POSIX compliant and works under Cygwin. I haven't tried it
under pythonw.

Just install cgywin (including python) then follow the standard
instructions for pexpect.

There was one small trick I had to do to get cygwin working totally
properly on my machine which was run a rebaseall.

Rebaseall sets the memory addresses for the DLLs or something like that.

However, there is a slight problem. The rebaseall runs inside cygwin and
uses one of the DLLs. To get around this I change the rebaseall script
to write it's command to a text file and then run those commands in a
DOS cmd shell. After that everything has worked without problem.


Good luck,

Paul
Paul,
I did ceck out the PExpect, however, I thought it was not ported for
Windows. Did you find a ported version? If not, what did you have to
do to be able to use it?

Thanks

Paul Dale wrote:

You might want to try python expect which gives you a very simple and
scriptable interface to a process.

http://pexpect.sourceforge.net/

I've been using it on windows to automate a few things.

Cheers,

Paul

jas wrote:


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:




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.





import subprocess as sp
p = sp.Popen("cmd", stdout=sp.PIPE)

result = p.communicate("ipconfig")




'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
 
J

jas

Paul said:
pexpect is POSIX compliant and works under Cygwin. I haven't tried it
under pythonw.

Well if I want my code to run on other computers, then they'd have to
run it under Cygwin right?
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top