473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 8475
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on...
8
by: Darren Dale | last post by:
Is there a place to put python programs so I dont have to refer to the absolute path everytime I want to call them? For example: if I am in /home/me and want to execute: python...
2
by: timdoyle05 | last post by:
Hi, I have a question relating to how Unix commands can be issued from Python programs. Im am currently writing a large test script in python and I need this script to call three other separate...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
7
by: Brian Erhard | last post by:
I am still fairly new to python and wanted to attempt a home made password protection program. There are files that I carry on a USB flash drive that I would like to password protect. ...
5
by: ivarnelispam | last post by:
Hello all, I'm starting work on what is going to become a fairly substantial Python project, and I'm trying to find the best way to organize everything. The project will consist of: - A few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.