Commands from within python script not found

时间:2018-03-25 20:17:40

标签: python command subprocess call

I am working with the sox commandline tool to convert a file inside a python script. I am able to convert the file manually from a command prompt window, but when I execute my code form the same working directory I am returned with an error:

'sox' is not recognized as an internal or external command, operable program or batch file.

sox is my path. But not being found. Is it possible when python running the subprocess.call method the current working directory is changed for that method, and is run in a location where some of my path variables are not being found?

This is how I am running my code that does not find "sox"

    print("Enter a name for the .wav output file")
    filename = raw_input()
    + " " + filename + ".wav"
    COMMAND = "sox " + fileOut + " " + filename + ".wav"
    subprocess.call(COMMAND, shell = True)

If i make my command this it executes correctly...

COMMAND = "C:\\\"Program Files (x86)\"\sox-14-4-2\sox.exe " + fileOut + " " + filename + ".wav"

Why is sox not being found from within this python file?

1 个答案:

答案 0 :(得分:1)

Drop your shell=True and make COMMAND a list:

COMMAND = ["sox", fileOut, filename + ".wav"]
subprocess.call(COMMAND)