f



os.system always opens new window on Windows XP/2000

Can anyone tell me how to run a bunch of commands in the same command
line window?  When I do this:

system("command1")
system("command2")
....

it opens a new window for every system call.  The alternative I can
think of is to write out the command to a batch file and run that, but
I'm wondering if there is a better way to do it.

Partly it is annoying to have 20 cmd windows flash on the screen, but
also I don't think it works if I do something like this:

system("set envvar=hi")

since I think the envvar does not persist once the command window is
closed.

thanks!
0
1/6/2004 6:45:59 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

5 Replies
676 Views

Similar Articles

[PageSpeed] 3

John <ohgoddearlord@yahoo.com> wrote in message
news:d0132d7d.0401061045.73c28639@posting.google.com...
> Can anyone tell me how to run a bunch of commands in the same command
> line window?  When I do this:
>
> system("command1")
> system("command2")
> ...

If you just want to hide the console window, try
"win32process.CreateProcess"
If you want to set environment variables, I think batch file is OK.

Best  Regards,
Kent Hsu

>
> it opens a new window for every system call.  The alternative I can
> think of is to write out the command to a batch file and run that, but
> I'm wondering if there is a better way to do it.
>
> Partly it is annoying to have 20 cmd windows flash on the screen, but
> also I don't think it works if I do something like this:
>
> system("set envvar=hi")
>
> since I think the envvar does not persist once the command window is
> closed.
>
> thanks!


0
cphsu (4)
1/7/2004 8:33:17 AM
Is there a reason why there is no way to run all the commands in the same
session?  This is how the perl system function or `` works.

MB

> If you just want to hide the console window, try
> "win32process.CreateProcess"
> If you want to set environment variables, I think batch file is OK.
>
> Best  Regards,
> Kent Hsu
>
> >
> > it opens a new window for every system call.  The alternative I can
> > think of is to write out the command to a batch file and run that, but
> > I'm wondering if there is a better way to do it.
> >
> > Partly it is annoying to have 20 cmd windows flash on the screen, but
> > also I don't think it works if I do something like this:
> >
> > system("set envvar=hi")
> >
> > since I think the envvar does not persist once the command window is
> > closed.
> >
> > thanks!
>
>


0
crap4559 (10)
1/7/2004 4:59:13 PM
"Moosebumps" <crap@crud.com> wrote in message
news:BVWKb.6639$FE3.206@newssvr27.news.prodigy.com...
> Is there a reason why there is no way to run all the commands in the same
> session?  This is how the perl system function or `` works.

The system() function is, as the doc says, simply an interface
to the C library's system() function, with all the limitations that
implies.

You could certainly do it if you wanted to, but it's not the
default. You'd have to use one of the popen# functions
in the os module to start a shell and then feed it commands.

As to why it's not there, I suppose nobody has gotten around
to writing it and submitting it for evaluation for inclusion in the
core. That may say something about its perceived utility. Then
again, it may not.

You might want to join the discussion on PEP 324.

John Roth
>
> MB
>


0
newsgroups10 (865)
1/7/2004 6:33:52 PM
ohgoddearlord@yahoo.com (John) wrote in message news:<d0132d7d.0401061045.73c28639@posting.google.com>...
> Can anyone tell me how to run a bunch of commands in the same command
> line window?  When I do this:
> 
> system("command1")
> system("command2")
> ...
> 

Use os.popen("commandx")
This will return a file-like object from which you can read.
e.g. print os.popen("commandx").read()
will give you the console-output of "commandx".
But you must know what you're doing!
e.g. os.popen("command") will start a console-window which
you won't see -- because you used this to avoid a console-window -- 
and after all I know, there's no chance to come back from this command.

> it opens a new window for every system call.  The alternative I can
> think of is to write out the command to a batch file and run that, but
> I'm wondering if there is a better way to do it.
> 
> Partly it is annoying to have 20 cmd windows flash on the screen, but
> also I don't think it works if I do something like this:
> 
> system("set envvar=hi")

This command has no effect. You start a process in whose enviroment
%envvar% exists, but if you don't do anything in the context of this
process after the statement os.system("set envvar=hi") %envvar% will
be forgotten.
Better do: os.environ('envvar']= 'hi'
Now the context is your python-script and all you do after this
statement will know %envvar% until your script will exit.

> 
> since I think the envvar does not persist once the command window is
> closed.

True

> 
> thanks!

Regards
Peter
0
PeterAbel (58)
1/7/2004 10:06:27 PM
On 6 Jan 2004 10:45:59 -0800, ohgoddearlord@yahoo.com (John) wrote:

>Can anyone tell me how to run a bunch of commands in the same command
>line window?  When I do this:
>
>system("command1")
>system("command2")
>...
>
>it opens a new window for every system call.  The alternative I can
>think of is to write out the command to a batch file and run that, but
>I'm wondering if there is a better way to do it.

I would recommend reading a little more about the os, shutil, and
system modules and doing it all in Python.  I would especially advise
you to do that if you're seriously considering a batch file instead.
Unless you're going to have people without Python run the same batch
file, you may as well build your script in Python, which is something
it is touted for being good at.
    --dang
0
noemail25 (101)
1/9/2004 12:03:16 PM
Reply: