# running non-python progs from python

Discussion in 'Python' started by Spiffy, Dec 9, 2003.

1. ### SpiffyGuest

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?

Spiffy, Dec 9, 2003

2. ### Michel Claveau/HamsterGuest

Hi !

You can also do (sample) :

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

@+
--
Michel Claveau

Michel Claveau/Hamster, Dec 9, 2003

3. ### Fredrik LundhGuest

"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-questions.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>

Fredrik Lundh, Dec 9, 2003
4. ### SpiffyGuest

"Michel Claveau/Hamster" <> wrote in
message news:br3t6s$44i$...
> 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?

Spiffy, Dec 9, 2003
5. ### Fredrik LundhGuest

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

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

</F>

Fredrik Lundh, Dec 9, 2003
6. ### Dennis Lee BieberGuest

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

--
> ============================================================== <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ============================================================== <

Dennis Lee Bieber, Dec 9, 2003
7. ### SpiffyGuest

"Fredrik Lundh" <> wrote in message
news:...
> "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.
> 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

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

Spiffy, Dec 9, 2003
8. ### Michel Claveau/HamsterGuest

Du calme, s'il vous plaît...

--
Michel Claveau

Michel Claveau/Hamster, Dec 9, 2003
9. ### SpiffyGuest

"Dennis Lee Bieber" <> wrote in message
news:...
> 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".

============================================================== <
> > | Wulfraed Dennis Lee Bieber KD6MOG <
> > | Bestiaria Support Staff <
> > ============================================================== <

>

Spiffy, Dec 9, 2003
10. ### Diez B. RoggischGuest

> 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

Diez B. Roggisch, Dec 9, 2003
11. ### SpiffyGuest

"Diez B. Roggisch" <> wrote in message
news:br5hu8$em2$01$-online.com... > > 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 > There is no space missing. That is the name of the file on my comp. Spiffy, Dec 9, 2003 12. ### SpiffyGuest Fredrik Lundh" <> wrote in message news:... > "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. > Spiffy, Dec 9, 2003 13. ### Diez B. RoggischGuest >> > 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 >> > There is no space missing. That is the name of the file on my comp. Ok, most probably you'll start to yell at me now, but _there is a space missing_: >>> filename = "C:\Python22\Canyon.mid" >>> print "C:\Python22\playb.exe%s"%filename C:\Python22\playb.exeC:\Python22\Canyon.mid I have no WinBox here - but I bet even windows likes its commandline arguments somehow separated from each other. Try executing the line above, and I bet command.com complains about "Bad command or file name". Diez Diez B. Roggisch, Dec 9, 2003 14. ### FedorGuest "Spiffy" <> wrote in message news:2fsBb.423999$...
>
> "Diez B. Roggisch" <> wrote in message
> news:br5hu8$em2$01$-online.com... > > > 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 > > > There is no space missing. That is the name of the file on my comp. > > The ^ points right after exe. (use fixed fonts to see). The command you are executing is: C:\Python22\playb.exeC:\Python22\Canyon.mid There needs to be a space between exe and the second C: Hope it helps. Fedor, Dec 9, 2003 15. ### SpiffyGuest "Diez B. Roggisch" <> wrote in message news:br5k2i$a6s$02$-online.com...
> >> > 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
> >>

> > There is no space missing. That is the name of the file on my comp.

>
> Ok, most probably you'll start to yell at me now, but _there is a space
> missing_:
>
> >>> filename = "C:\Python22\Canyon.mid"
> >>> print "C:\Python22\playb.exe%s"%filename

> C:\Python22\playb.exeC:\Python22\Canyon.mid
>
> I have no WinBox here - but I bet even windows likes its commandline
> arguments somehow separated from each other. Try executing the line above,
> and I bet command.com complains about "Bad command or file name".
>
> Diez
>
>

....why would i yell at you? you are trying to help and i appreciate that.
Unfortunately, I have tried various spacings both in the filename and the
os.system call and what happens most often is that a dos box appears and
hangs and python stops responding.

Spiffy, Dec 10, 2003
16. ### SpiffyGuest

"Fedor" <> wrote in message
news:3fd66012$0$113$... > > "Spiffy" <> wrote in message > news:2fsBb.423999$...
> >
> > "Diez B. Roggisch" <> wrote in message
> > news:br5hu8$em2$01\$-online.com...
> > > > 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
> > >

> > There is no space missing. That is the name of the file on my comp.
> >
> >

>
> The ^ points right after exe. (use fixed fonts to see). The command you

are
> executing is:
>
> C:\Python22\playb.exeC:\Python22\Canyon.mid
>
> There needs to be a space between exe and the second C:
>
> Hope it helps.
>
>

I have tried your suggestion and put a space after the .exe...the result is
the same as before...a dos box appears and does nothing and python crashes
(stops responding).

Spiffy, Dec 10, 2003
17. ### Dennis Lee BieberGuest

Spiffy fed this fish to the penguins on Tuesday 09 December 2003 14:06
pm:

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

If that is the true statement you are using then it is NOT the same as
the command line shown above... THERE'S NO SPACE BETWEEN PROGRAM and
ARGUMENT!

You are trying to run a file named:

C:\Python22\playb.exeC:\Python22\Canyon.mid

--
> ============================================================== <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ============================================================== <

Dennis Lee Bieber, Dec 10, 2003
18. ### Fredrik LundhGuest

Spiffy wrote:

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

>
> IT COULD HAVE BEEN A MISPRINT, DON'T YOU THINK?

sure. if multiple independent sources say the same thing, your
first thought should be that they're all wrong.

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

> >
> > you mean sys.path?

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

the python path is stored in the sys.path variable. why did you
say python path if you didn't mean it?

> I DO NOT THINK THE PROBLEM IS IN HOW I USED THE FUNCTION
> NOR WHAT I EXPECTED FROM IT.

no, your problems are obviously elsewhere. good luck with your
programming career.

</F>

Fredrik Lundh, Dec 10, 2003
19. ### SpiffyGuest

"Fredrik Lundh" <> wrote in message
news:...
> Spiffy wrote:
>
> > > that indicates that it's supposed to work, don't you think?

> >
> > IT COULD HAVE BEEN A MISPRINT, DON'T YOU THINK?

>
> sure. if multiple independent sources say the same thing, your
> first thought should be that they're all wrong.
>
> > > > Both the .exe and the .mid file are in the python path and the

spelling
> > > > been checked.
> > >
> > > you mean sys.path?

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

>
> the python path is stored in the sys.path variable. why did you
> say python path if you didn't mean it?
>
> > I DO NOT THINK THE PROBLEM IS IN HOW I USED THE FUNCTION
> > NOR WHAT I EXPECTED FROM IT.

>
> no, your problems are obviously elsewhere. good luck with your
> programming career.
>
> </F>

It's interesting to me that, although you provided no help or answers to me,
you are convinced that you did. In fact, it is clear from your first
response that your intention has been to amuse yourself by spouting attitude
at me. Hope you had a good time. Meanwhile, Fredo has provided a nice answer
that WORKS... along with an explanation of why my code didn't work. He was
HELPFUL and didn't seem to have the need to belittle me for not being a
professional Python Master. You remind me of one of these teens on IRC who
have no other pleasure in life but to sit around and boot people out of
their precious little chat rooms. I imagine if this was IRC, you would have
booted me at the first post.

Spiffy, Dec 10, 2003
20. ### Dennis Lee BieberGuest

Dennis Lee Bieber fed this fish to the penguins on Wednesday 10
December 2003 00:31 am:

Talking to myself, again...
>
>
> Spiffy fed this fish to the penguins on Tuesday 09 December 2003 14:06
> pm:
>
>
>> os.system("C:\Python22\playb.exe%s"%filename)

>

What result do you get with just:

os.system("dir")

--
> ============================================================== <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ============================================================== <