By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,492 Members | 1,284 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,492 IT Pros & Developers. It's quick & easy.

running non-python progs from python

P: n/a
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?
Jul 18 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
Hi !

You can also do (sample) :

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

@+
--
Michel Claveau


Jul 18 '05 #2

P: n/a
"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>


Jul 18 '05 #3

P: n/a

"Michel Claveau/Hamster" <No********@No.Spam.mclaveau.No.Spam.com> wrote in
message news:br**********@news-reader2.wanadoo.fr...
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 youdo, 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?

Jul 18 '05 #4

P: n/a
"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>


Jul 18 '05 #5

P: n/a
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()

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #6

P: n/a

"Fredrik Lundh" <fr*****@pythonware.com> wrote in message
news:ma**************************************@pyth on.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?


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.

Jul 18 '05 #7

P: n/a
Du calme, s'il vous plaît...

--
Michel Claveau
Jul 18 '05 #8

P: n/a

"Dennis Lee Bieber" <wl*****@ix.netcom.com> wrote in message
news:ef************@beastie.ix.netcom.com...
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".
================================================== ============ < > wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wu******@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <

Jul 18 '05 #9

P: n/a
> 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

Jul 18 '05 #10

P: n/a

"Diez B. Roggisch" <de************@web.de> wrote in message
news:br*************@news.t-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.
Jul 18 '05 #11

P: n/a
Fredrik Lundh" <fr*****@pythonware.com> wrote in message
news:ma**************************************@pyth on.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?


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.


Jul 18 '05 #12

P: n/a
>> > 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
Jul 18 '05 #13

P: n/a

"Spiffy" <sp****@worldnet.att.net> wrote in message
news:2f***********************@bgtnsc04-news.ops.worldnet.att.net...

"Diez B. Roggisch" <de************@web.de> wrote in message
news:br*************@news.t-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.
Jul 18 '05 #14

P: n/a

"Diez B. Roggisch" <de************@web.de> wrote in message
news:br*************@news.t-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.
Jul 18 '05 #15

P: n/a

"Fedor" <fe***@mailandnews.com> wrote in message
news:3f*********************@reader2.nntp.hccnet.n l...

"Spiffy" <sp****@worldnet.att.net> wrote in message
news:2f***********************@bgtnsc04-news.ops.worldnet.att.net...

"Diez B. Roggisch" <de************@web.de> wrote in message
news:br*************@news.t-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).
Jul 18 '05 #16

P: n/a
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
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #17

P: n/a
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>


Jul 18 '05 #18

P: n/a

"Fredrik Lundh" <fr*****@pythonware.com> wrote in message
news:ma************************************@python .org...
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.
Jul 18 '05 #19

P: n/a
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")

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #20

P: n/a
"Spiffy" wrote:
Meanwhile, Fredo has provided a nice answer that WORKS...
along with an explanation of why my code didn't work.


for the benefit of others, maybe you could tell us why
your code didn't work?

</F>


Jul 18 '05 #21

P: n/a

"Spiffy" <sp****@worldnet.att.net> wrote in message
news:IH**********************@bgtnsc05-news.ops.worldnet.att.net...

"Dennis Lee Bieber" <wl*****@ix.netcom.com> wrote in message
news:ef************@beastie.ix.netcom.com...
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".

Try:
import.os
os.system(r"C:\Python22\playb.exe %s"%filename)

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

-Mark

================================================== ============ < > wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wu******@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #22

P: n/a
or:
import os
os.system("C:\\Python22\\playb.exe %s"%filename)


Thank you. Fedor had emailed me yesterday with this solution and a nice
explanation. Thanks to all who tried to help.
Jul 18 '05 #23

P: n/a
In article <6t**********************@bgtnsc05-news.ops.worldnet.att.net>,
Spiffy <sp****@worldnet.att.net> wrote:
or:
import os
os.system("C:\\Python22\\playb.exe %s"%filename)


Thank you. Fedor had emailed me yesterday with this solution and a nice
explanation. Thanks to all who tried to help.


Did anyone ever disclose the solution publicly? This
case has me curious; os.system() misunderstandings
often arise, and I'd like to learn more about what can
go wrong.
--

Cameron Laird <cl****@phaseit.net>
Business: http://www.Phaseit.net
Jul 18 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.