0

Now, I'm working on a voice controlling assistant application.

When I say "open Google Chrome", it should open Chrome. I do not have any problems with speech recognition but I do have a problem with starting the program.

For example, the following code:

import os
os.system("chrome.exe")

Works fine for the notepad.exe or cmd.exe programs but when I give external programs like chrome, I need to give the entire path.

However, in C# we can only give the direct name like chrome.exe to run the program.

So, my problem is that, is there any ways to start external programs without giving the entire path like in C#?

Giving path to start the program would be a serious problem. Because when we move the program to another computer, we will face many code errors.

nnnmmm
  • 7,964
  • 4
  • 22
  • 41

4 Answers4

0

Try os.path.abspath

os.path.abspath("chrome.exe")

returns the following: C:\Users\Yourname\chrome.exe

Thimo1
  • 99
  • 5
0

The first part of this answer is OS specific and doesn't solve it in code or python

Add the path where Chrome exists to your PATH variable (you'll likely need a restart), and then when you execute a command such as chrome.exe it will try to run it from that path location

UPDATE: If you want to implement a search for the absolute path here are some good resources on path listing How do I list all files of a directory?

But again you'll likely need some educated guesses and will need to do some OS specific things to find it.

The Chrome binary is often installed in the same set of locations

sedavidw
  • 11,116
  • 13
  • 61
  • 95
  • But, When I move the program to another computer. It won't work. I do not only have to start chrome.exe. I have to start more than 15 external programs. – Guna Rakulan Apr 03 '19 at 18:29
  • Yes that's true but then you'll either need to 1. Know where Chrome will be on every computer (I'd assume in this case you'll do it in the same place) or 2. Somehow search for it (I can't imagine this is ideal) TLDR is the machine you're trying to execute the command on needs to have all the information and at some point that includes the path in one way or another – sedavidw Apr 03 '19 at 18:59
0

The PATH system variable governs this inside Python just as outside. If you want to modify it from Python, it's os.environ.

As an aside, a better solution is probably to use subprocess instead, as suggested in the os.system documentation.

The PATH manipulation will be the same regardless.

import os
import subprocess

os.environ['PATH'] = os.environ['PATH'] + r';c:\google\chrome\'
subprocess.run(['chrome.exe'])

... where obviously you need to add the directory which contains chrome.exe to your PATH.

Probably you will want to make sure the PATH is correct even before running Python in your scenario, though.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • But, when I move the program to another computer. It won't work, if the chrome installs in a different directory. – Guna Rakulan Apr 03 '19 at 18:33
  • That's what the last paragraph means; you will need to set up the `PATH` to point to the correct binaries one way or another. – tripleee Apr 03 '19 at 18:35
0

I solved this problem by using C#. Like I already mentioned, we can directly call programs like "chrome.exe" in C#. I created a console application in C# which open Google Chrome via using bellow code. Then I complied that console application as .exe, after that I link that exe file with my python program.

C#

Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "chrome.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();

Python

import os
os.system('console_application.exe')