2

When I run a terminal command in python, it throws an error. Below is the code I run from a .py-file in terminal:

import time
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
   if(GPIO.input(12) == 0):
      os.system ('omxplayer /home/pi/Downloads/bell.mp3')
      print("DOOR BELL RINGING")
      os.system ("cd /home/pi/picam")
      os.system ("./picam --alsadev hw:1,0 -w 640 -h 360 -v 800000 -f 24 --tcpout $")
      time.sleep(1)

This code is for video streaming, but it doesn't work. The error message is:

sh: 1: ./picam: not found
3
  • I have had trouble with calling bash-files (like your picam) as well. Usually it helps to insert "bash" in front of the command.. Would you mind posting your picam-file? Nov 28, 2016 at 11:26
  • 2
    ./picam: not found means it isn't in the directory or it is but doesn't have execute permissions.
    – joan
    Nov 28, 2016 at 11:38
  • Is picam in the directory where you are running the command from? You can verify this by doing ls -la from the command line and looking for the file in the output. Please edit your question and include the output of ls -la. is picam the entire file name or is it picam.sh? Nov 28, 2016 at 15:12

1 Answer 1

4

Note that the two separate calls of os.system are exactly that - completely separate from each other. Whatever happens in the first call is lost and forgotten in the second (like tears in rain).

os.system ("cd /home/pi/picam")
os.system ("./picam --alsadev ...")

Changing to /home/pi/picam is of no relevance to the next call which leads to the expected result - the error that is thrown - not found. Simplest solution here: use the full path /home/pi/picam/picam --alsadev ... instead of ./picam --alsadev ....

From https://docs.python.org/3/library/os.html#os.system it is recommended to use subprocess over os.system:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

1
  • In other words, when you run the script it should do the cd itself.
    – SDsolar
    Apr 21, 2017 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.