Search code examples
pythonsubprocesspeewee

Subprocess returns exit status 1


When I run a command through subprocess I get exit status 1 without my print or the error raised.

here is my code:

    def generate_model(self):
        if not ((self.username == None) or (self.password == None) or (self.database == None)):
            cmd = "python -m pwiz -e %s -H %s -u %s -P %s %s > %s"%(self.engine,self.host,self.username,self.password,self.database,self.database+".py")
            print subprocess.check_call(cmd)
        else:
            raise ValueError

command asks an input once terminal is opened. After that it closes with exit status 1

When I run the same command directly in command prompt it works fine


Solution

  • subprocess.check_call() does not run the shell by default and therefore the redirection operator > won't work. To redirect stdout, pass stdout parameter instead:

    with open(filename, 'wb', 0) as file:
        check_call([sys.executable, '-m', 'pwiz', '-e', ...], stdout=file)
    

    Related: Python subprocess.check_output(args) fails, while args executed via Windows command line work OK.