简体   繁体   中英

How to run a shell script placed in different folder from python

I am using the subprocess.call like below:

subprocess.call(['sudo ./order_fc_prioritizer/run.sh'])

But its saying no such file or directory

您可以使用脚本的绝对(而不是相对)相对文件路径。

Pass a list to call() with the command and arguments split into separate list elements:

subprocess.call(['sudo', './order_fc_prioritizer/run.sh'])

See the documentation, section Frequently Used Arguments .

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (eg to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

You could also use shlex.split() to create the argument list:

import shlex

subprocess.call(shlex.split('sudo ./order_fc_prioritizer/run.sh'))

You might consider using subprocess.Popen()

Basically subprocess.call() is just an helper function to:

proc = subprocess.Popen(cmd, cwd=PATH)
proc.wait()

but Popen() has a keyword argument cwd that's the working directory where to execute the command (spawn the process).

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