Velocity Reviews > running non-python progs from python

# running non-python progs from python

Spiffy
Guest
Posts: n/a

 12-09-2003
hello, i'm fairly new to python programming and programming in general, but
i have written a python prog that creates a MIDI file (.mid) and I want to
be able to play it from another prog which is written in BASIC.
It's a command-line prog that takes the MIDI filename as a parameter and
plays the file.
The 'Learning Python' book talks about using the os.system call, but I
haven't been able to get this to work.
How can I run this other program from a python script and pass the filename
as a parameter?

Michel Claveau/Hamster
Guest
Posts: n/a

 12-09-2003
Hi !

You can also do (sample) :

import os
vret = os.popen('c:\\pfiles\\lplayer.exe')
print vret

@+
--
Michel Claveau

Fredrik Lundh
Guest
Posts: n/a

 12-09-2003
"Spiffy" wrote:

> The 'Learning Python' book talks about using the os.system call, but I
> haven't been able to get this to work.

"it doesn't work" is a really lousy way to describe a problem what did you
do, what happened, and what did you expect would happen instead?

also see:

http://www.catb.org/~esr/faqs/smart-...ons.html#intro

> 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>

Spiffy
Guest
Posts: n/a

 12-09-2003

"Michel Claveau/Hamster" <(E-Mail Removed)> wrote in
message news:br3t6s$44i$(E-Mail Removed)...
> Hi !
>
> You can also do (sample) :
>
> import os
> vret = os.popen('c:\\pfiles\\lplayer.exe')
> print vret
>
>
>
> @+
> --
> Michel Claveau

Hello Michel, thanks for taking the time to respond. Unfortunately, when I
tried to run your sample code (changing the name of the file, of course), it
did nothing.

> 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. When I run it, the dos command line appears with the
message 'Bad command or file name". Both the .exe and the .mid file are in
the python path and the spelling has been checked.
What I expected to happen was that the os.system call would start the .exe
and begin playing the .mid file. This does not happen. This is what I meant
when I said I haven't been able to get this to work.

>"it doesn't work" is a really lousy way to describe a >problem what did

you
>do, what happened, and what did you expect would >happen instead?
>
>also see:
>
> http://www.catb.org/~esr/faqs/smartquestions.html#intro
>

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

Fredrik Lundh
Guest
Posts: n/a

 12-09-2003
"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>

Dennis Lee Bieber
Guest
Posts: n/a

 12-09-2003
Spiffy fed this fish to the penguins on Tuesday 09 December 2003 00:38
am:

> from the "Learning Python" book. When I run it, the dos command line
> appears with the message 'Bad command or file name". Both the .exe and
> the .mid file are in the python path and the spelling has been
> checked. What I expected to happen was that the os.system call would
> start the .exe and begin playing the .mid file. This does not happen.
> This is what I meant when I said I haven't been able to get this to
> work.
>

So include (don't retype or paraphrase, use cut&paste) the /exact/
code which is failing, AND the exact command window output...

Among other things, I don't think os.system() uses the /Python/ path
-- its the equivalent of opening a new command prompt and typing the
command; so it is the OS search path that is used.

Open a command window, and type the exact command you think you are
using in os.system()

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <

Spiffy
Guest
Posts: n/a

 12-09-2003

"Fredrik Lundh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "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?

IT COULD HAVE BEEN A MISPRINT, 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?

IF I HAD MEANT sys.path, I WOULD HAVE SAID sys.path, DON'T YOU THINK?

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)

TYPING THE FULL PATH GIVES ME THE SAME RESULT: NOTHING. THE ABOVE CODE USING
os.path.abspath CAUSES A DOS BOX TO APPEAR AND DO NOTHING WHILE PYTHON
CRASHES.
> 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.

I DID NOT ASK ANYONE TO READ MY MIND, NOR DID I COMPLAIN THAT ANYONE COULD
NOT READ MY MIND.
I DO NOT THINK THE PROBLEM IS IN HOW I USED THE FUNCTION NOR WHAT I EXPECTED
FROM IT.
>

Michel Claveau/Hamster
Guest
Posts: n/a

 12-09-2003
Du calme, s'il vous plaît...

--
Michel Claveau

Spiffy
Guest
Posts: n/a

 12-09-2003

"Dennis Lee Bieber" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Spiffy fed this fish to the penguins on Tuesday 09 December 2003 00:38
> am:
>
> > from the "Learning Python" book. When I run it, the dos command line
> > appears with the message 'Bad command or file name". Both the .exe and
> > the .mid file are in the python path and the spelling has been
> > checked. What I expected to happen was that the os.system call would
> > start the .exe and begin playing the .mid file. This does not happen.
> > This is what I meant when I said I haven't been able to get this to
> > work.
> >

> So include (don't retype or paraphrase, use cut&paste) the /exact/
> code which is failing, AND the exact command window output...
>
> Among other things, I don't think os.system() uses the /Python/

path
> -- its the equivalent of opening a new command prompt and typing the
> command; so it is the OS search path that is used.
>
> Open a command window, and type the exact command you think you

are
> using in os.system()
>
> --
> >

In the command window the call to the program with the name of the .mid
file to be played works just fine. Here is what it looks like....exactly:
C:\Python22>playb Canyon.mid

Here is the code used to call it from Python:
import os
filename = "C:\Python22\Canyon.mid"
os.system("C:\Python22\playb.exe%s"%filename)
....this causes a dos box to appear which promptly hangs and does nothing, at
which time Python stops responding. Vartiations on this will cause the dos
box to appear with the message "Bad command or file name".

================================================== ============ <
> > (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> > (E-Mail Removed) | Bestiaria Support Staff <
> > ================================================== ============ <
> > Bestiaria Home Page: http://www.beastie.dm.net/ <

>

Diez B. Roggisch
Guest
Posts: n/a

 12-09-2003
> Here is the code used to call it from Python:
> import os
> filename = "C:\Python22\Canyon.mid"
> os.system("C:\Python22\playb.exe%s"%filename)
> ...this causes a dos box to appear which promptly hangs and does nothing,
> at which time Python stops responding. Vartiations on this will cause the
> dos box to appear with the message "Bad command or file name".
>

Looks like there is a space missing -

os.system("C:\Python22\playb.exe %s"%filename)
^

Diez