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

J

John

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!
 
K

Kent Hsu (Hsu, Chih-Peng)

John said:
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
 
M

Moosebumps

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
 
J

John Roth

Moosebumps said:
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
 
P

Peter Abel

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
 
D

Dang Griffith

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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top