faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 11 people (64%) answered Yes
Recently 5 of 9 people (56%) answered Yes

Entry

Running a program in the background (was: os.popen() and (IDLE, PythonWin) ?)

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 266, Fredrik Lundh


"""
Packages: operating_systems.windows;operating_systems.unix
"""
"""
> Ok, but how can I prevent the shell windows
> from popping up ? I read something about os.spawnv() in the docs,
> but the mode argument remains a complete mystery and I always
> get IOErrors about files not being found...
recently seen on c.l.py:
Subject: Re: How to eliminate consol window in MS Windows
Date: Thu, 28 Oct 2020 08:59:53 +0200
Radovan Garabik <garabik@center.fmph.uniba.sk.spam> wrote:
> and what is worse, the program does not run in the background
> I'd really like to use 
> os.spawnv(os.P_NOWAIT, "program.pyw", ("program.pyw", args))
> but it does not work at all... am I missing something?
> (I tried with the whole path, still nothing....)
> (note: I know next to nothing about programming under MS windows.. I am
> trying to do a quick port of my unix script)
you could try something like:
file = r"\full\path\pythonw.exe"
os.spawnv(os.P_NOWAIT, file, (file, "program.pyw") + tuple(args))
note that spawnv doesn't scan the path, and that
you need to pass the name of the excutable as the
first item in the argument tuple.
the following sample script from the eff-bot guide
shows one way to locate the executable:
"""
# File: os-spawn-example-2.py
import os
import string
def run(program, *args, **kw):
    # find executable
    mode = kw.get("mode", os.P_WAIT)
    for path in string.split(os.environ["PATH"], os.pathsep):
        file = os.path.join(path, program) + ".exe"
        try:
            return os.spawnv(mode, file, (file,) + args)
        except os.error:
            pass
    raise os.error, "cannot find executable"
run("python", "hello.py", mode=os.P_NOWAIT)
print "goodbye"