0

I'm getting this error:

Traceback (most recent call last): File "app.py", line 29, in print(get_active_window_title()) File "app.py", line 7, in get_active_window_title output = subprocess.check_output('./app.sh') File "/home/rishabh/anaconda2/lib/python2.7/subprocess.py", line 216, in check_output process = Popen(stdout=PIPE, *popenargs, **kwargs) File "/home/rishabh/anaconda2/lib/python2.7/subprocess.py", line 394, in init errread, errwrite) File "/home/rishabh/anaconda2/lib/python2.7/subprocess.py", line 1047, in _execute_child raise child_exception OSError: [Errno 13] Permission denied when running project code

when I'm trying to run this code on a fresh ubuntu install, which used to work perfectly before. chmod doesn't work. Following is the code

import sys
import os
import subprocess
import re
import cv2
def get_active_window_title():
    output = subprocess.check_output('./app.sh')
    if output.decode("utf-8") == "Desktop\n":
        return "Desktop"
    else:
        root = subprocess.Popen(['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=subprocess.PIPE)
        stdout, stderr = root.communicate()

        m = re.search(b'^_NET_ACTIVE_WINDOW.* ([\w]+)$', stdout)
        if m != None:
            window_id = m.group(1)
            window = subprocess.Popen(['xprop', '-id', window_id, 'WM_CLASS'], stdout=subprocess.PIPE)
            stdout, stderr = window.communicate()
        else:
            return None

        match = re.match(b'WM_CLASS\(\w+\) = ".*", (?P<name>.+)$', stdout)
        if match != None:
            return match.group("name").strip(b'"')

        return None

if __name__ == "__main__":
                print(get_active_window_title())
  • It looks like you have not properly changed the mode of `./app.sh` to executable. List the file with `-l` and double check to make sure that the process running your python script has `execute` permission on that `./app.sh`. Also, I would recommend using a list of a first argument to `check_output`; as in `subprocess.check_output(['./app.sh'])`. You should also use the full path of `app.sh` rather than relying on the current working directory `.`. – Abdou Jul 10 '20 at 16:00
  • I tried everything you said, changed permission to 755 on app.py as well as demo.py (the file calling it) but to no avail. I added square brackets and also typed full pathname but that doesn't change the error. How does reinstalling the same OS and prerequisites stop this code from working? – Rishabh Patel Jul 10 '20 at 17:00
  • *changed permission to 755 on app.py*? But your script is calling `./app.sh`. One is a `python` script while the other is a `SHELL` script. You need to set the permissions on the `app.sh` file in order for it to be invoked properly by `check_output`. – Abdou Jul 10 '20 at 17:35
  • Kindly guide me how to do so, I'm confused right now. Thanks for your patience. – Rishabh Patel Jul 11 '20 at 03:42
  • Both `chmod +x ./app.sh` and `chmod +x ./app.py` should be invoked. The scripts `app.py` and `app.sh` need to be executable for you to be able to pass either of them to `subprocess.check_output`. In your case, you have to make sure that `app.sh` is executable at least. – Abdou Jul 11 '20 at 15:46

0 Answers0