How to run external program?

G

Gary Herron

Lad said:
How can I run external program from Python?
I use Python with XP
Thank you for help
LB
The subprocess module is what you want for this.

It's got ways of running external executables as separate subprocesses,
and interacting with the subprocess and both its input and output.

Gary Herron
 
L

Lad

Gary said:
The subprocess module is what you want for this.

It's got ways of running external executables as separate subprocesses,
and interacting with the subprocess and both its input and output.
Gary ,
Thank you for your reply.
I use os.popen3 but it does not work properly for me all the time.

Here is a part of my Python program
I use an external program ( here mpeg.exe) for converting sourcefile
into targetfile
###########...
....
....
mpeg = "mpeg.exe -i %s codec mp3 -s 320x240 %s" % (sourcefile,
targetfile)
stdin, stdout, stderr = os.popen3(mpeg)
mpegresult = stdout.read()
mpegerrors = stderr.read()
stdin.close(); stdout.close(); stderr.close()
print ffmpegerrors
print ffmpegresult
#########

It works if the sourcefile is small but if it is large( 30MB) it does
NOT work.It hangs or the file is not converted in full.
Any advice how I should change the program?
Thank you.
L.
 
V

Viewer T.

Lad said:
How can I run external program from Python?
I use Python with XP
Thank you for help

A possible way to do this is the following:
Let's say we want to run a program called program.exe from the path
C/Program/Program.exe. We would first need to import the os module and
type the following:

import os #import the os module
path = 'C:/Program/Program.exe' #give the pathname of the external
program as a string
os.system(path) #execute the program from
python using the os module

There are many other ways, but this should work for short pathnames
without spaces.
 
G

Gabriel Genellina

mpeg = "mpeg.exe -i %s codec mp3 -s 320x240 %s" % (sourcefile,
targetfile)
stdin, stdout, stderr = os.popen3(mpeg)
mpegresult = stdout.read()
mpegerrors = stderr.read()
stdin.close(); stdout.close(); stderr.close()
print ffmpegerrors
print ffmpegresult
#########

It works if the sourcefile is small but if it is large( 30MB) it does
NOT work.It hangs or the file is not converted in full.
Any advice how I should change the program?

What do you mean by "NOT work"?
Does it "work" if you execute the same line from the command prompt?
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top