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? 14 9416
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
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....7e8e2a3?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?
Any other ideas? or examples of using subprocess to do what I was
asking?
Kent Johnson wrote: 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....7e8e2a3?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?
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, captures the output of the commands and saves it to a file.
What did I miss?
Kent
Kent Johnson wrote:
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....7e8e2a3?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?
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: 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
Kent Johnson wrote:
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....7e8e2a3?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?
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.
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: 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, captures the output of the commands and saves it to a file.
What did I miss?
Kent
Kent Johnson wrote:
>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....7e8e2a3?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? >>
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.
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.
Ideas?
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/
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: 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.
Ideas? 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/
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
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
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
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
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
jas wrote: 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
Paul Dale wrote: 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel Hasler |
last post by:
Hi
With JPDA (Java Platform Debugging Architecture), is it possible to
redirect System.out statements from the remote VM to the debugger
client application, and if yes, how do I do it?
- Dan
|
by: Frank Rizzo |
last post by:
Is there anyway to transparently redirect the output of the
Debug.WriteLine statement from the Debug window to something else such
as a console or a file?
Thanks
|
by: sriparna |
last post by:
Hi all,
After "dtterm" how do I redirect my output to the new terminal instead
of the old one?
|
by: JohnF |
last post by:
I'd like to capture the stdout output from system("command")
in a buffer. Although system("command >tmpnam") and then
open,read,remove tmpnam works, it's a bit more messy than
I'd like. Is there...
|
by: loketing |
last post by:
Is there any way to redirect the output of the system() function? I know there is for redirecting stdout, but I can't make this work with system().
I tried to store the output of a system command...
|
by: lazydev |
last post by:
Here's my code just redirect the output to a variable or out statement
any suggestions
alter proc r (@id INT)
as
BEGIN
DECLARE @input VARCHAR(800)
DECLARE @c_input INT
DECLARE ...
|
by: sriram8383 |
last post by:
Hi
I am connecting Unix from Java Program and running a Db2 SQL command which perform the storage/redirect the output value to the specified file.
Database server = DB2/AIX64 8.2.9
...
|
by: dmitrey |
last post by:
hi all,
could anyone post an example how to redirect text output (stdout) to
Text widget?
Thank you in advance,
Dmitrey.
|
by: Gabriel Genellina |
last post by:
En Sat, 15 Nov 2008 07:27:47 -0200, Indian <write2abdul@gmail.com>
escribió:
(I don't get what the above code is supposed to perform)
You're right, unit tests should run without user...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |