Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing command line programs from within python [duplicate]

I'm building a web application that will is going to manipulate (pad, mix, merge etc) sound files and I've found that sox does exactly what I want. Sox is a linux command line program and I'm feeling a little uncomfortable with having the python web app starting new sox processes on my server on a per request basis.

Example:

import os os.system('sox input.wav -b 24 output.aiff rate -v -L -b 90 48k') 

This whole setup seems a little unstable to me.

So my question is, what's the best practice for running command line programs from within a python (or any scripting language) web app?

Message queues would be one thing to implement in order to get around the whole request response cycle. But is there other ways to make these things more elegant?

like image 712
Mattias Avatar asked Jan 16 '09 12:01

Mattias


People also ask

How do you run a command in subprocess in Python?

Python Subprocess Run Function The subprocess. run() function was added in Python 3.5 and it is recommended to use the run() function to execute the shell commands in the python program. The args argument in the subprocess. run() function takes the shell command and returns an object of CompletedProcess in Python.

How do you call Python shell from the command line?

To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write python and press enter. A Python Prompt comprising of three greater-than symbols >>> appears, as shown below. Now, you can enter a single statement and get the result.


1 Answers

The subprocess module is the preferred way of running other programs from Python -- much more flexible and nicer to use than os.system.

import subprocess #subprocess.check_output(['ls', '-l'])  # All that is technically needed... print(subprocess.check_output(['ls', '-l'])) 
like image 128
dF. Avatar answered Sep 21 '22 01:09

dF.