user5826447 user5826447 - 29 days ago 5
Python Question

Pass python argument to shell

In the python notebook I would like to pass the temp argument in the first line to the last line, but am not sure how to do that.

def grabdata(statefolders, temp, split_by):
for folder in statefolders:
sub = folder.split('_')[split_by]
new_name = sub + '_out.txt'
!cp {folder}/done/sigdet_output*out temp{new_name}

Answer

If

!cp {folder}/done/sigdet_output*out temp{new_name}

is what you would usually execute in your shell, the command in Python would be:

import subprocess
subprocess.run(["cp", "{}/done/sigdet_output*out".format(folder), "temp{}".format(new_name)])