OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Getting 'psutil.AccessDenied (pid=6836)' error using psutil

  • Thread starter Thread starter Nima Metana
  • Start date Start date
N

Nima Metana

Guest
I want to check if a program is running or not, so I searched and I tried this , in this way:

Code:
import psutil
if "notepad.exe" in (p.name() for p in psutil.process_iter()):
    print('program is running')
else:
    print('not running')

but I get this error:

Code:
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/temp.py", line 2, in <module>
    if "notepad.exe" in (p.name() for p in psutil.process_iter()):
  File "~/temp.py", line 2, in <genexpr>
    if "notepad.exe" in (p.name() for p in psutil.process_iter()):
  File "~\Python\Python38-32\lib\site-packages\psutil\__init__.py", line 630, in name
    name = self._proc.name()
  File "~\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 750, in name
    return os.path.basename(self.exe())
  File "~\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 681, in wrapper
    raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=6836)

Process finished with exit code 1

am I doing something wrong with this simple code? or is there another alternative way?

EDIT: sorry, i forgot to mention im on windows, not linux, can u give me solutions for windows platform?
<p>I want to check if a program is running or not, so I searched and I tried <a href="https://stackoverflow.com/a/7788702/13518924">this</a> , in this way:</p>

<pre><code>import psutil
if "notepad.exe" in (p.name() for p in psutil.process_iter()):
print('program is running')
else:
print('not running')
</code></pre>

<p>but I get this error:</p>

<pre class="lang-none prettyprint-override"><code>During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "~/temp.py", line 2, in <module>
if "notepad.exe" in (p.name() for p in psutil.process_iter()):
File "~/temp.py", line 2, in <genexpr>
if "notepad.exe" in (p.name() for p in psutil.process_iter()):
File "~\Python\Python38-32\lib\site-packages\psutil\__init__.py", line 630, in name
name = self._proc.name()
File "~\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 750, in name
return os.path.basename(self.exe())
File "~\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 681, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=6836)

Process finished with exit code 1
</code></pre>

<p>am I doing something wrong with this simple code?
or is there another alternative way? </p>

<p>EDIT: sorry, i forgot to mention im on windows, not linux, can u give me solutions for windows platform?</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top