1

I'm trying to run a bash script from a Python script. Both this bash script and python script reside in the same directory. However, when I run the Python script, I get No such file or directory error.

My current directory

xxx@xxx:/home# ls
check.sh sc.py 

Following is the code I'm trying to run, sc.py:

import subprocess
subprocess.Popen(["check.sh", "ABC"])

I'm getting the following error:

xxx@xx:/home# python3 sc.py
Traceback (most recent call last):
  File "sc.py", line 2, in <module>
    subprocess.Popen(["check.sh", "ABC"])
  File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'check.sh': 'check.sh'

What am I doing wrong here?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
randomUser
  • 61
  • 1
  • 8
  • 1
    Does this answer your question? [How to call a shell script from python code?](https://stackoverflow.com/questions/3777301/how-to-call-a-shell-script-from-python-code) – mkrieger1 Aug 16 '23 at 08:25
  • `check.sh` is not an executable. – mkrieger1 Aug 16 '23 at 08:25
  • @mkrieger1, I am wondering, does it have to be executable? I remember I have called a bash script without it being executable before. – randomUser Aug 16 '23 at 08:40
  • It doesn't have to have its executable bit set if you run it like this `bash check.sh` but that is not how you are running it, you are just running it with `check.sh` which means 1) it must have its executable bit set and 2) it must be on your PATH. – Mark Setchell Aug 16 '23 at 08:59
  • I think you need to specify the directory, i.e. `./check.sh`. How else should `subprocess`, respectively the shell, know, where to find the file? – user1934428 Aug 16 '23 at 11:50
  • If you run a command just by its name, say `x`, then the file x must be executable, and it must reside in one of the directories mentioned in your PATH. This is fundamental for Linux/Unix, and I guess that Python's _subprocess_ either adheres to this convention, or executes a shell (POSIX shell) for doing the work, in which case the shell adheres to this convention. – user1934428 Aug 16 '23 at 11:54

1 Answers1

0

There is no file check.sh in PATH. If you want to find something in current working directory, find it there.

subprocess.Popen(["./check.sh", "ABC"])

If you want to find something in the same directory as python script, navigate to the same directory as the python script.

import subprocess
from pathlib import Path
subprocess.Popen([str(Path(__file__).parent / "check.sh"), "ABC"])
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I have tried your approach `import subprocess subprocess.Popen([str(Path(__file__).parent / "check.sh"), "ABC"]) `. Same error. – randomUser Aug 16 '23 at 08:41
  • Did you try the first? Try adding `.absolute()`. And, also, execute the shell explicitly. The erro may come because the executable in shebang in the script does not exists. Did you read the linked question in the comment above? – KamilCuk Aug 16 '23 at 08:45
  • @KamilCuk same error will be thrown if the shebang on the start of the check.sh is not pointing to valid interpreter. – rasjani Aug 16 '23 at 08:55