1

Possible Duplicate:
How do I execute a program from python? os.system fails due to spaces in path

I am trying to call a program (MP3gain.exe) in command line from python. my problem is that python puts a [' '] around the command that I am sending to command line, and dos doesn't appear to be able to interpret the command with that. Here is my code.

import os
import subprocess
import Editor

class normalize():
    def __init__(self, file):
        self.FileName = file

    def work(self):
        command = [ 'mp3gain /r /c' + self.FileName]
        subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)


if __name__ == "__main__":
    test = normalize("filename.mp3")
    test.work()

In case this helps, if I have dos print out the exit code, it is -2. Thanks for any help.

Community
  • 1
  • 1
user1432738
  • 101
  • 1
  • 5
  • 3
    What do you mean `python puts a [' '] around the command`? You're not supposed to have an actual snake type the code for you, it's just the language name. – Lev Levitsky Nov 21 '12 at 21:35
  • If your command line should be `mp3gain /r /c filename.mp3` - have you tried `command = ['mp3gain', '/r', '/c', self.FileName]` ? (also mp3gain will have to be callable from the interpreter's working directory - otherwise, you may need to call the path for the full executable) – Jon Clements Nov 21 '12 at 21:36

2 Answers2

3

command should be a list of strings, with mp3gain as the first one, that is:

command = ['mp3gain', '/r', '/c', self.FileName]
martineau
  • 119,623
  • 25
  • 170
  • 301
mike
  • 403
  • 3
  • 6
0

You can call a program by using os.system. For example in you program you can use:

os.system("mp3gain /r /c " + self.FileName)
  • 1
    os.system is old. It is recommended to use subprocess to replace all the old methods for running a process. – jdi Nov 22 '12 at 01:06