Can't open sh file

18,949

Solution 1

I do not believe this would run from the terminal either because you must run the file in order. Try:

os.system('sh chmod +x run.sh|./run.sh')

instead.

See: https://askubuntu.com/questions/38661/how-do-i-run-sh-files for details on running sh files and how to use os.system() in python for running an shell order for the use of | running in an order in a shell.

Solution 2

Make sure that your bash script has the right permissions (i.e it is executable). In a terminal run:

chmod +x run.sh

and then try (assuming that run.sh is in the same directory as your python script)

import os
os.system('./run.sh')
Share:
18,949

Related videos on Youtube

R.Anchieta
Author by

R.Anchieta

Updated on June 04, 2022

Comments

  • R.Anchieta
    R.Anchieta almost 2 years

    I'm trying to execute a sh file from script python.

    My script python

    os.system('sh run.sh')
    

    My sh file

    echo 'The house is blue' | /opt/palavras/por.pl > output.txt
    

    Error:

    sh: 0: Cant' open run.sh

    How can I fix it?

    • Aaron
      Aaron over 6 years
      You might want to use an absolute path or a path relative to the directory from which you run the python program. As it is currently, sh is going to look for run.sh in its $PATH, which might differ from your execution context's $PATH.
  • Xantium
    Xantium over 6 years
    As I thought you need to use the "or" | operator run in a shell order. os.system('chmod +x run.sh'), os.system('./run.sh') doesn't do it unfortunately because this is equivalent to opening two terminals.