I have a question regarding my Python code. It seems that once I start QIV using os.system, the code stops. This is proved by my code below. Once the code is ran and I press the button on GPIO 17, the "Hello" is not printed into the terminal. Only after I close QIV, it prints it. There is not much else to say, thank you for reading this! If you need any more information, please tell me.


import RPi.GPIO as GPIO
import time
import os

GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def slideShow(channel):
    os.system("sudo qiv -w 100 *.jpg")

GPIO.add_event_detect(17, GPIO.FALLING, callback = slideShow, bouncetime = 2000)

GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def kill(channel):
    os.system("sudo pkill qiv")

GPIO.add_event_detect(4, GPIO.FALLING, callback = kill, bouncetime = 2000)

while 1:

Error: Unable to read file: Failed to open file '*.jpg': No such file or directory.

  • if you don't want your call to qiv to block you should use psopen instead of os.system as described in this question and answer stackoverflow.com/questions/636561/… Mar 24, 2017 at 7:04
  • try escaping the * Mar 24, 2017 at 7:15
  • @SteveRobillard Sorry, what I just told you was wrong. The problem is that it cannot recognize the files in the folder anymore. Mar 24, 2017 at 7:17
  • @MadMike I put the error up. Mar 24, 2017 at 17:24
  • As the error says, qiv can't find any find any jpg Files. Check if the files are there and if you are running your script from the right directory.
    – MadMike
    Mar 27, 2017 at 7:33

1 Answer 1


Switch away from os.system(...) to subprocess.popen(...)

The way you wan't to use os.system(...) has two crucial problems:

  1. It won't return until the command has was finished.
  2. It doesn't open a shell and thus won't expand *.jpg to the available files ending with jpg in your current directory.

Both and those problems can be solved by using subprocess.popen(...) as in:

import subprocess
subprocess.Popen('qiv -w 100 *.jpg', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Find a solution that doesn't need root-privileges

Check if you really need root rights. Make an effort to change your system so you don't.

If there is a error within your script, the same script with root-rights might be a leverage to give a hacker an entry point to your whole system.

If you still need root rights, use it only for the parts of the script that really needs it.

  • The part about sudo is not entirely true: the OP may be better off without sudo, but for different reasons. sudo is smart enough to detect a non-interactive call (plus there's a -n switch if you want to be sure). And running the whole script as root makes thinks even worse from security perspective. Apr 3, 2017 at 9:44
  • 1
    @DmitryGrigoryev Thanks for the input. I've changed that part. I'm open for more improvements.
    – MadMike
    Apr 3, 2017 at 11:49

Your Answer

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

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