Reputation: 49
I need to find the location of the web-browser in the program I'm making.
I've decided to do this by running a browser window then finding the path of it. I've looked at psutil but still can't figure out how to do it.
I'm doing this cause I can't seem to open a new window using the webbrowser library, it opens in a new tab regardless of wherever I tell it to or not. So I plan to use the commands explained here: http://kb.mozillazine.org/Command_line_arguments#List_of_command_line_arguments_.28incomplete.29
I'm using Python 3.8.6 on Windows 10
Upvotes: 1
Views: 1784
Reputation: 309
Link to original code (psutil)
I modified original code to meet your needs. Unfortunately I cannot test them since I am on Android platform and psutil module in Termux's Python can't access (since Android update) stats due security/privacy reasons so if they don't work please tell me
Simple approach
import psutil
def findPath(name: str) -> list:
ls: list = [] # since many processes can have same name it's better to make list of them
for p in psutil.process_iter(['name', 'pid']):
if p.info['name'] == name:
ls.append(psutil.Process(p.info['pid']).exe())
return ls
More advanced
import os
import psutil
def findPathAdvanced(name: str) -> list:
ls: list = [] # same reason as previous code
for p in psutil.process_iter(["name", "exe", "cmdline", "pid"]):
if name == p.info['name'] or p.info['exe'] and os.path.basename(p.info['exe']) == name or p.info['cmdline'] and p.info['cmdline'][0] == name:
ls.append(psutil.Process(p.info['pid']).exe())
return ls
Upvotes: 1
Reputation: 49
Found a solution using psutil finally!
import psutil
def findPath(name):
for pid in psutil.pids():
if psutil.Process(pid).name() == name:
return psutil.Process(pid).exe()
print(findPath('firefox.exe'))
This loops through all pids and checks to see if the pids name is the same as the name variable passed to the findPath function.
Upvotes: 2