2

I am creating a program in python 3 and i would like the user to open a URL with a specified web browser application.I have tried using the subprocess.Popen([application, URL]) but it raises a FileNotFoundError. Any help is greatly appreciated.

EDIT: I forgot to mention that I am using Windows 10, and here is a copy of the Error message I am getting:

Traceback (most recent call last):
File "C:\Users\[Name]\Desktop\AI.py", line 221, in <module>
subprocess.Popen(["google-chrome", "www.google.co.uk/"])
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

EDIT2: This is my result if i try running subprocess.Popen(["start", "chrome", "www.example.com/"]) (and i get the same error if i leave out the "start", part of the array):

`Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    subprocess.Popen(["start", "chrome", "http://www.google.co.uk/"])
  File "C:\Python34\lib\subprocess.py", line 859, in __init__ restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified`
3
  • 1
    What is the name of the application? It tried subprocess.Popen(["google-chrome", "http://example.com/"]) on Ubuntu and it works fine. Commented Feb 6, 2016 at 19:34
  • i was using "chrome.exe" on windows, but I have just tried using "google-chrome" and "google-chrome.exe" and still got the same error message Commented Feb 6, 2016 at 20:54
  • Can you open chrome.exe from the command-line without Python? Otherwise there may some problem with your PATH. Try to give Popen the full path of chrome (driver letter and folders) and see if that works. Commented Feb 6, 2016 at 21:09

2 Answers 2

6

Or you can use the "webbrowser" module

    import webbrowser

    url = 'www.google.com'

    webbrowser.open_new(url)

It will use your default browser, no need to look for your machine' specific browser path. Makes it a lot more versatile and usable outside your workspace.

Sign up to request clarification or add additional context in comments.

Comments

4
import subprocess

subprocess.Popen([r"C:/Users/haral_000/AppData/Local/Google/Chrome/Application/chrome.exe", "example.com"])

The 'r' at the start of the string makes it a raw string so that the backslashes aren't interpreted as escapes.

Not the most elegant solution maybe, but it works. I went to to the Start menu and searched for Chrome, right-clicked and selected "Open file location", then viewed Properties of the shortcut to find the actual location of the exe file. No doubt, this file is in another location for you. And certainly not under my username.

3 Comments

I've tried this, but i got another FileNotFoundError (see EDIT2)
See my new edit! I tested this on my Windows 7 virtual machine.
I've got this to work using the full file path: subprocess.Popen([r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "www.google.co.uk"]) I had to navigate to it's executable in order to get it to work

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.