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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
linkyndy
  • 17,038
  • 20
  • 114
  • 194
  • The problem can also be the shebang-line in your script.sh. Maybe it points to a wrong interpreter, this way you get the same error message. Just a thought… – tamasgal Nov 18 '14 at 15:25
  • `#!/bin/bash` is the shebang-line – linkyndy Nov 18 '14 at 15:26
  • 1
    OK, if `/bin/bash` exists, then you should look at `envoy`, I don't know about that package. Try a `envoy.run('pwd')` to check where you are. – tamasgal Nov 18 '14 at 15:27
  • Have you used `chmod` on the script already? I don't know exactly what `envoy` does but it might still possibly require that your script is in `$PATH`. – WGS Nov 18 '14 at 15:29
  • 1
    A wrong chmod setting (missing x-flag) would yield a "Permission denied". – tamasgal Nov 18 '14 at 15:30

2 Answers2

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)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Indeed, specifying the `cwd` to `envoy` **and** `chmod`-ing `scripts.sh` solved my issue. Thank you! – linkyndy Nov 19 '14 at 08:35
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