简体   繁体   English

从特定文件夹的python运行.sh脚本

[英]Run .sh script from python from specific folder

I am trying to run a .sh script from python.我正在尝试从 python 运行 .sh 脚本。

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.但是,这假设 test.sh 位于您从中运行脚本的同一文件夹中。 What if I want to run the .sh which is in a specific folder?如果我想运行特定文件夹中的 .sh 怎么办?

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) subprocess.call有一个cwd函数参数(更改工作目录)

import subprocess

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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