running non-python progs from python

S

Spiffy

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

Michel Claveau/Hamster

Hi !

You can also do (sample) :

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



@+
 
F

Fredrik Lundh

Spiffy said:
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>
 
S

Spiffy

Michel Claveau/Hamster said:
Hi !

You can also do (sample) :

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



@+

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

Fredrik Lundh

Spiffy said:
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>
 
D

Dennis Lee Bieber

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

--
 
S

Spiffy

Fredrik Lundh said:
that indicates that it's supposed to work, don't you think?

IT COULD HAVE BEEN A MISPRINT, DON'T YOU THINK?
that indicates that Windows cannot find the command, don't you think?


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


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

Spiffy

Dennis Lee Bieber said:
Spiffy fed this fish to the penguins on Tuesday 09 December 2003 00:38
am:

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


============================================================== <
 
D

Diez B. Roggisch

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
 
S

Spiffy

Diez B. Roggisch said:
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.
 
S

Spiffy

Fredrik Lundh said:
that indicates that it's supposed to work, don't you think?

IT COULD HAVE BEEN A MISPRINT, DON'T YOU THINK?
that indicates that Windows cannot find the command, don't you think?


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


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

Diez B. Roggisch

filename = "C:\Python22\Canyon.mid"
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_:
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
 
F

Fedor

Spiffy said:
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.
 
S

Spiffy

Diez B. Roggisch said:
Ok, most probably you'll start to yell at me now, but _there is a space
missing_:

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

Spiffy

Fedor said:
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).
 
D

Dennis Lee Bieber

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


--
 
F

Fredrik Lundh

Spiffy said:
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.
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>
 
S

Spiffy

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


the python path is stored in the sys.path variable. why did you
say python path if you didn't mean 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.
 
D

Dennis Lee Bieber

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:

What result do you get with just:

os.system("dir")

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top