简体   繁体   中英

Run .sh script from python from specific folder

I am trying to run a .sh script from python.

I saw that this can be done in various ways such as:

import subprocess

subprocess.call(["./test.sh"])

or

import os 

os.system("sh test.sh")

However this assumes that test.sh is in the same folder where you are running the script from. What if I want to run the .sh which is in a specific folder?

I tried the following but with no luck:

import subprocess

subprocess.call(["cd ~/ros_ws", "./intera.sh"])

import subprocess

subprocess.call(["cd ~/ros_ws", "./intera.sh"], shell=True)

Thanks for the help.

The subprocess.call has an cwd function argument (change working directory)

import subprocess

subprocess.call(["./intera.sh"], cwd="~/ros_ws")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM