Python - Execute more programs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krisztian Kepes

    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:
    [color=blue][color=green][color=darkred]
    >>>>>>>>>>>>> >>[/color][/color][/color]

    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(Fi leName),PChar(S L[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:\Pyth on\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



  • Gerhard Häring

    #2
    Re: Python - Execute more programs

    Egor Bolonev wrote:[color=blue]
    > 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(). [...][/color]

    No, because os.system blocks.

    Invoking the .py file directly with os.spawnv won't work.
    "Executabil ity" (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\p ython.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

    Comment

    • Egor Bolonev

      #3
      Re: Python - Execute more programs

      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> "Executabil ity" (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\p ython.exe", ["python", \
      GH> r"c:\path\to\my \python\script. py"])

      =os.system('pyt hon '+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: [email protected]


      Comment

      • Bengt Richter

        #4
        Re: Python - Execute more programs

        On Mon, 30 Jun 2003 14:59:20 +0200, "Krisztian Kepes" <Kepes.Krisztia [email protected]> wrote:
        [color=blue]
        >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:
        >[color=green][color=darkred]
        >>>>>>>>>>>>>>> >[/color][/color]
        >
        >program startmore;
        >
        >uses
        > Windows,
        > Classes,
        > SHELLAPI;
        >
        >var
        > SL:TStringList;
        > FileName:string ;i:integer;
        >begin
        > SL:=3DTStringLi st.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(Fi leName),PChar(S L[i]),nil,sw_SHOW);
        > end;=20
        > finally
        > SL.Free;
        > end;
        >end.
        >
        >In the param (sm.txt) file:
        >
        >Neurolotto.p y
        >-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.ex e sm.txt[/color]

        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:=3DTStringLi st.Create;
        # try
        try:
        # if (ParamCount<1) then Exit;
        if not sys.argv[1:]: raise SystemExit,'Usa ge: [python] %s filepath' % sys.argv[0]
        # SL.LoadFromFile (ParamStr(1));
        SL = file(sys.argv[1]).read().splitl ines() # 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(Fi leName),PChar(S L[i]),nil,sw_SHOW);
        os.system('star t %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>sta rtmore.py
        Usage: [python] C:\pywk\clp\sta rtmore.py filepath

        -- That was with no args, now the real thing:
        [18:46] C:\pywk\clp>sta rtmore.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.

        [color=blue]
        >
        >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:\P ython\python.ex e'
        >filename=3D' c:/homedev/neurolotto/bin/v1.0/Neurolotto.py'
        >for params in parlist:
        > s=3D'"'+filenam e+' '+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 ?
        >[/color]
        Or a little more compactly, if you like:

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

        Regards,
        Bengt Richter

        Comment

        Working...