0

I am trying to run something inside a Python file, and i do not want to see in the Python console the run output:

os.system("cd ../programs/{0} && ./run".format(project))

How can I do it? I tried with subprocess.call() but it does not compile.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    *How* did you try with `subprocess.call()`? – Charles Duffy Mar 12 '20 at 17:46
  • What do you mean by "subprocess.call() but it does not compile"? Python isn't compiled and the subprocess module is a standard library. So what actually happens when you use it? Because I think using it is your answer right there. – Ted Klein Bergman Mar 12 '20 at 17:46
  • Also, you aren't actually doing any redirection here. Where do you want to redirect output to? – Charles Duffy Mar 12 '20 at 17:47
  • @TedKleinBergman Most Python implementations are indeed compiled, just not to native machine code. CPython compiles Python byte code (analogous to Java compiling to Java byte code). – chepner Mar 12 '20 at 17:52
  • @chepner Yes, that's true. I meant more in the sense that you don't run a separate compilation step where compilations error and runtime errors are disjunct. In hindsight, this distinction is not necessary for the problem at hand. – Ted Klein Bergman Mar 12 '20 at 17:57

1 Answers1

3

Redirecting stdout to /dev/null:

import subprocess

subprocess.call(['./run'],
                cwd=os.path.join('..', 'programs', project),
                stdout=subprocess.DEVNULL)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 3
    Also note: `os.path.join('..', 'programs', project)` would be a more canonical way of constructing the path. Or if you like modern Python tools more, `pathlib.Path('..', 'programs', project)`. – ShadowRanger Mar 12 '20 at 17:48
  • Huh, I never realized `subprocess` had added `DEVNULL`. – chepner Mar 12 '20 at 17:49
  • 1
    Also, `subprocess.run` is now recommended over `subprocess.call`. – chepner Mar 12 '20 at 17:51
  • I have tried to run the following: ``` subprocess.run(['./run'], cwd=pathlib.Path('..', 'programs', project), stdout=subprocess.DEVNULL) ``` – Enrique González Mar 14 '20 at 12:28
  • And i get the following error (I have copy only the last part of the error): ''' subprocess.run(['./run'], cwd=pathlib.Path('..', 'programs', project), stdout=subprocess.DEVNULL) File "/usr/lib/python3.6/subprocess.py", line 423, in run with Popen(*popenargs, **kwargs) as process: File "/usr/lib/python3.6/subprocess.py", line 729, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) OSError: [Errno 8] Exec format error: './run' ''' – Enrique González Mar 14 '20 at 12:32
  • @EnriqueGonzález, that typically means your script doesn't have a shebang as its first line. Add one. (`#!/bin/sh` if it's written for POSIX sh, `#!/usr/bin/env bash` to choose the first versions of bash in the PATH, etc). – Charles Duffy Mar 14 '20 at 14:50