Re: running non-python progs from python

From: Fredrik Lundh (fredrik_at_pythonware.com)
Date: 12/09/03


Date: Tue, 9 Dec 2003 12:43:59 +0100
To: python-list@python.org


"Spiffy" wrote:

> > How can I run this other program from a python script and pass the
> filename
> > as a parameter?
>
> import os
>
> filename = "myfile"
> os.system("program %s" % filename)
>
> </F>
>
> Fredrik, the example you provided is virtually the same as the one from the
> "Learning Python" book.

that indicates that it's supposed to work, don't you think?

> When I run it, the dos command line appears with the message 'Bad command
> or file name".

that indicates that Windows cannot find the command, don't you think?

> Both the .exe and the .mid file are in the python path and the spelling has
> been checked.

you mean sys.path? that's the problem, most likely. As mentioned in the
documentation, os.system() executes the command just as if you've typed
it in a "DOS box". Windows doesn't look at the Python path when you do
that, so to make sure Windows finds the command, you have to add it to
the Windows path (the PATH environment variable), or provide the full path
to os.system().

    program = r"x:\full\path\to\program"
    filename = "..."
    os.system("%s %s" % (program, filename))

the os.path.abspath() function can be useful in cases like this; it makes sure
that if your program can find a file, another program can also find it:

    program = os.path.abspath("x:/somewhere/program")
    filename = os.path.abspath("somefile")
    os.system("%s %s" % (program, filename))

(if the filename may contain spaces, you may have to add quotes around
the second %s)

the relevant manual page contains more information on os.system, and
mentions a couple of alternatives (os.startfile, os.spawnlp, etc):

    §http://www.python.org/doc/current/lib/os-process.html

> Pardon me for being a newbie, but if you don't have an answer, why do you
> have to give me attitude?

os.system() is still the answer; the problem is in how you used it and what
you expected from it, not in the function itself. You cannot expect people
to read your mind, and then complain when they fail.

</F>



Relevant Pages

  • Re: Quando Apple lascia le cose al caso...
    ... " keep a backup file ... " make all windows the same size when adding/removing windows ... " remove '=' from the list of valid filename chars. ...
    (it.comp.macintosh)
  • Re: How to pass short filename in context menu?
    ... So in the command field in the registry change the path to cmd to something explorer can't find. ... Windows 95/98/Me: The Windows system directory. ... > would pass the long filename in a context menu shell command. ...
    (microsoft.public.windowsxp.general)
  • Re: running non-python progs from python
    ... > that indicates that Windows cannot find the command, ... > that, so to make sure Windows finds the command, you have to add it to ... > (if the filename may contain spaces, you may have to add quotes around ... I DID NOT ASK ANYONE TO READ MY MIND, NOR DID I COMPLAIN THAT ANYONE COULD ...
    (comp.lang.python)
  • Re: del command and auto-named file disaster
    ... still have the original file "Copy of FILENAME". ... Hint: Delete command looked ... anything I can do e.g. a Windows tweak that allows Delete command to ... It is designed for users and has the safety net ...
    (microsoft.public.windows.file_system)
  • Re: More blanks in filenames
    ... I do know windows ... character of the filename in the above 'set' command appears as ... I don't think you have a tab in the filename. ...
    (comp.lang.tcl)