Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to "executable" parameter of the subprocess.Popen() call

The subprocess.Popen() lets you pass the shell of your choice via the "executable" parameter.
I have chosen to pass "/bin/tcsh", and I do not want the tcsh to read my ~/.cshrc.
The tcsh manual says that I need to pass -f to /bin/tcsh to do that.

How do I ask Popen to execute /bin/tcsh with a -f option?

import subprocess

cmd = ["echo hi"]
print cmd

proc = subprocess.Popen(cmd, shell=False,  executable="/bin/tcsh", stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()

for line in proc.stdout:
    print("stdout: " + line.rstrip())

for line in proc.stderr:
    print("stderr: " + line.rstrip())

print return_code
like image 269
Shajid Thiruvathodi Avatar asked Sep 19 '25 12:09

Shajid Thiruvathodi


1 Answers

Make your life easier:

subprocess.Popen(['/bin/tcsh', '-f', '-c', 'echo hi'],
    shell=False, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
like image 151
Michael Wild Avatar answered Sep 22 '25 07:09

Michael Wild