469,646 Members | 1,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Python - Execute more programs

Hi !

I want to start many py programs - with other parameters.

Because the programs have many to-do, I don't want to wait for them.

In Delphi it is like this:
>>>>>>>>>>>


program startmore;

uses
Windows,
Classes,
SHELLAPI;

var
SL:TStringList;
FileName:string;i:integer;
begin
SL:=TStringList.Create;
try
if (ParamCount<1) then Exit;
SL.LoadFromFile(ParamStr(1));
FileName:=SL[0];
for i:=1 to SL.Count-1 do begin
ShellExecute(0,'open',PChar(FileName),PChar(SL[i]),nil,sw_SHOW);
end;
finally
SL.Free;
end;
end.

In the param (sm.txt) file:

Neurolotto.py
-W1 -S1
-W2 -S1
-W3 -S1
-W4 -S1
-W5 -S1
-W1 -S2
-W2 -S2
-W3 -S2
-W4 -S2
-W5 -S2
-W1 -S3
-W2 -S3
-W3 -S3
-W4 -S3
-W5 -S3
-W1 -S4
-W2 -S4
-W3 -S4
-W4 -S4
-W5 -S4
-W1 -S5
-W2 -S5
-W3 -S5
-W4 -S5
-W5 -S5
startmore.exe sm.txt

The shell is doing his work good, it is find the py -> python.exe assoc., start the py in other cmd window, etc.

But I want to write it in py.

import os

parlist=['-W1 -S1','-W2 -S1' ]
pypath='c:\Python\python.exe'
filename='c:/homedev/neurolotto/bin/v1.0/Neurolotto.py'
for params in parlist:
s='"'+filename+' '+params+'"'
print pypath+' '+s
os.spawnv(os.P_NOWAIT,pypath,[s])

But it is not working. Startfile is seems to good (assoc !), but it is not handle the arguments.

How I do an Delphi compatible but Python based program ?

Thx:
KK

Jul 18 '05 #1
3 8341
Egor Bolonev wrote:
Hello, Krisztian!
You wrote on Mon, 30 Jun 2003 14:59:20 +0200:

KK> I want to start many py programs - with other parameters.

KK> Because the programs have many to-do, I don't want to wait for them.

[Sorry, skipped]
KK> os.spawnv(os.P_NOWAIT,pypath,[s])

May be you should use os.system(). [...]


No, because os.system blocks.

Invoking the .py file directly with os.spawnv won't work.
"Executability" (lol, which word) of .py files is a feature of the
Windows shell, but the deeper layers of the OS don't know about file
associations.

Instead, try something like:

os.spawnv(os.P_NOWAIT, r"c:\python22\python.exe", ["python", \
r"c:\path\to\my\python\script.py"])

It's an annoying detail that you have to repeat the name of the process
in the 3rd argument of the os.spawn* call on win32. I wasted quite some
time in finding this out once.

FWIW os.startfile() will work on win32 as well as long as you don't need
to give any additional parameters. This function uses the parts of the
Windows API that do have knowledge about file associations.

It's a mess. It's Windows ;-)

-- Gerhard

Jul 18 '05 #2
Hello, Gerhard!
You wrote on Mon, 30 Jun 2003 18:22:17 +0200:
[Sorry, skipped]

GH> Invoking the .py file directly with os.spawnv won't work.
GH> "Executability" (lol, which word) of .py files is a feature of the
GH> Windows shell, but the deeper layers of the OS don't know about file
GH> associations.

GH> Instead, try something like:

GH> os.spawnv(os.P_NOWAIT, r"c:\python22\python.exe", ["python", \
GH> r"c:\path\to\my\python\script.py"])

=os.system('python '+pyFileName),
P_NOWAIT=fork?

GH> It's an annoying detail that you have to repeat the name of the process
GH> in the 3rd argument of the os.spawn* call on win32. I wasted quite some
GH> time in finding this out once.

GH> FWIW os.startfile() will work on win32 as well as long as you don't
GH> need to give any additional parameters. This function uses the parts of
GH> the Windows API that do have knowledge about file associations.

GH> It's a mess. It's Windows ;-)
With best regards, Egor Bolonev. E-mail: eb******@rol.ru
Jul 18 '05 #3
On Mon, 30 Jun 2003 14:59:20 +0200, "Krisztian Kepes" <Ke*************@peto.hu> wrote:
Hi !

I want to start many py programs - with other parameters.

Because the programs have many to-do, I don't want to wait for them.

In Delphi it is like this:
>>>>>>>>>>>>

program startmore;

uses
Windows,
Classes,
SHELLAPI;

var
SL:TStringList;
FileName:string;i:integer;
begin
SL:=3DTStringList.Create;
try
if (ParamCount<1) then Exit;
SL.LoadFromFile(ParamStr(1));
FileName:=3DSL[0];
for i:=3D1 to SL.Count-1 do begin
ShellExecute(0,'open',PChar(FileName),PChar(SL[i]),nil,sw_SHOW);
end;=20
finally
SL.Free;
end;
end.

In the param (sm.txt) file:

Neurolotto.py
-W1 -S1
-W2 -S1
-W3 -S1
-W4 -S1
-W5 -S1
-W1 -S2
-W2 -S2
-W3 -S2
-W4 -S2
-W5 -S2
-W1 -S3
-W2 -S3
-W3 -S3
-W4 -S3
-W5 -S3
-W1 -S4
-W2 -S4
-W3 -S4
-W4 -S4
-W5 -S4
-W1 -S5
-W2 -S5
-W3 -S5
-W4 -S5
-W5 -S5
startmore.exe sm.txt


Ok, I'll just sneak it into the delphi-made-comments ;-)

====< startmore.py >===========================================
# program startmore;
#
# uses
# Windows,
# Classes,
# SHELLAPI;
import sys, os
#
# var
# SL:TStringList;
# FileName:string;i:integer;
# begin
# SL:=3DTStringList.Create;
# try
try:
# if (ParamCount<1) then Exit;
if not sys.argv[1:]: raise SystemExit,'Usage: [python] %s filepath' % sys.argv[0]
# SL.LoadFromFile(ParamStr(1));
SL = file(sys.argv[1]).read().splitlines() # gets rid of \n's
# FileName:=3DSL[0];
fileName = SL[0]
# for i:=3D1 to SL.Count-1 do begin
for i in xrange(1,len(SL)):
# ShellExecute(0,'open',PChar(FileName),PChar(SL[i]),nil,sw_SHOW);
os.system('start %s %s' % (fileName, SL[i]))
# end;=20
# finally
finally:
# SL.Free;
pass # should take care of itself
# end;
# end.
#
================================================== =============
Then we'll prefix your file with a line to generate cmd windows
and echo the parameters:

====< startmore.dat >===================
cmd /k echo
Neurolotto.py
-W1 -S1
-W2 -S1
-W3 -S1

==========================================

Running it does the expected on windows NT4 using python 2.2.2:

[18:46] C:\pywk\clp>startmore.py
Usage: [python] C:\pywk\clp\startmore.py filepath

-- That was with no args, now the real thing:
[18:46] C:\pywk\clp>startmore.py startmore.dat

[18:46] C:\pywk\clp>
-- back after starting a bunch of separate cmd windows, all persisting. In order:

====
Neurolotto.py

[18:46] C:\pywk\clp>
====
-W1 -S1

[18:46] C:\pywk\clp>
====
-W2 -S1

[18:46] C:\pywk\clp>
====
-W3 -S1

[18:46] C:\pywk\clp>
====
ECHO is on.

[18:46] C:\pywk\clp>
===

That last was from letting the final blank line pass through as an empty argument list.


The shell is doing his work good, it is find the py -> python.exe assoc., =
start the py in other cmd window, etc.

But I want to write it in py.

import os

parlist=3D['-W1 -S1','-W2 -S1' ]
pypath=3D'c:\Python\python.exe'
filename=3D'c:/homedev/neurolotto/bin/v1.0/Neurolotto.py'
for params in parlist:
s=3D'"'+filename+' '+params+'"'
print pypath+' '+s
os.spawnv(os.P_NOWAIT,pypath,[s])

But it is not working. Startfile is seems to good (assoc !), but it is not =
handle the arguments.

How I do an Delphi compatible but Python based program ?

Or a little more compactly, if you like:

====< startmore2.py >=====================
import sys, os
if not sys.argv[1:]: raise SystemExit,'Usage: [python] %s filepath' % sys.argv[0]
SL = file(sys.argv[1]).read().splitlines() # gets rid of \n's
fileName = SL.pop(0)
for argLine in SL: os.system('start %s %s' % (fileName, argLine))
==========================================
HTH

Regards,
Bengt Richter
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Maboroshi | last post: by
2 posts views Thread by timdoyle05 | last post: by
8 posts views Thread by Paul Cochrane | last post: by
118 posts views Thread by 63q2o4i02 | last post: by
7 posts views Thread by Brian Erhard | last post: by
5 posts views Thread by ivarnelispam | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.