Call customized bash function in Python

Viewed 33

I have searched online a lot and failed to find the exact workaround for this question. My question is that I would like to call a customized function in python. Say, I have written a bash function in my .bashrc

`

cpput2(){
    # 1 job name
    # 2 hpss pat
    # 3 filepath
    bsub -N -n 1 -q hpss -W 24:00 -P CWWW0001 -J $1 hsi "cd $2; cput -R $3"  
}

`

Is there any way to call it in python? os.system cannot find this function even I put it into my bashrc.

1 Answers

You can execute commands in bash over the command option -c. To make the function available in bash, you must include the source file with the dot command first, e.g.:

bash -c '. file.sh && cpput2 myjob myhpsspat mypath'

Note: Due to the and operator (&&), the function is only executed if the include of file.sh was successful.

You can call your function from python with os.system:

os.system("bash -c '. file.sh && cpput2 myjob myhpsspat mypath'")
Related