Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call multiple bash functions using | in python

I am using a scientific software (called vasp) that works only in bash, and using Python to create a script that will make multiple runs for me. When I use subprocess.check_call to call the function normally, it works fine, but when i add the '| tee tee_output' it doesn't work.

subprocess.check_call('vasp') #this works
subprocess.check_call('vasp | tee tee_output') #this doesn't

I am a noobie to python and programming altogether.

like image 976
mrkent Avatar asked Feb 22 '23 05:02

mrkent


2 Answers

Try this. It executes the command (passed as a string) via a shell, instead of executing the command directly. (It's the equivalent of calling the shell itself with the -c flag, i.e. Popen(['/bin/sh', '-c', args[0], args[1], ...])):

subprocess.check_call('vasp | tee tee_output', shell=True)

But attend to the warning in the docs about this method.

like image 102
senderle Avatar answered Mar 06 '23 04:03

senderle


You could do this:

vasp = subprocess.Popen('vasp', stdout=subprocess.PIPE)
subprocess.check_call(('tee', 'tee_output'), stdin=vasp.stdout)

This is generally safer than using shell=True, especially if you can't trust the input.

Note that check_call will check the return code of tee, rather than vasp, to see whether it should raise a CalledProcessError. (The shell=True method will do the same, as this matches the behavior of the shell pipe.) If you want, you can check the return code of vasp yourself by calling vasp.poll(). (The other method won't let you do this.)

like image 34
Taymon Avatar answered Mar 06 '23 05:03

Taymon