Search code examples
pythonbashshellsubprocesspython-envoy

Execute bash file from Python


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.


Solution

  • 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)