0

I want to execute a shell script file from within Python. I am currently using Envoy to do this:

envoy.run('./scripts.sh')

But it throws me a No such file or directory error.

I am wondering, under which path is the above file executed? How can I make the above script run? It is located in the same directory as the Python script.

4

2 回答 2

4

The program is executed in the current working directory as reported by os.getcwd(). For a command line program, its typically the directory you are in when you run the program. To run a command in the same directory as your python script, use the __file__ variable to figure out where you are:

import os
import envoy

my_path = os.path.dirname(os.path.abspath(__file__))
envoy.run('./scripts.sh', cwd=my_path)
于 2014-11-18T16:44:17.220 回答
1

The error you are receiving may not necessarily be from python itself but your bash script as the error you posted also can be returned from bash itself..

-bash: ./asdf: No such file or directory

Does your shell script require arguments? Also, have you tried to execute your shell script directly and not via python?

Another thing I would recommend is to use the environment bash rather than full path as this is typically better suited for portability due to the variances in linux OS design..

#!/usr/bin/env bash
于 2014-11-18T17:28:59.787 回答