Home > Software engineering >  How can I store a link in a variable and open it using os.system()
How can I store a link in a variable and open it using os.system()

Time:10-24

I want to make an app in Python to open links the user assigned to buttons. For that I have to assign links to variables and open them. I tried opening them with the webbrowser module, but they would open in Microsoft Edge and not in the default browser, so I decided to use os.system(), but I can't seem to succeed in opening the links in the variables. Here is the code:

import os
from sys import platform
link1 = "example.com"
if platform == "win32":
    os.system("start \"\" link1")
elif platform == "linux" or "linux2":
    os.system("xdg-open \"\" link1")
elif platform == "darwin":
    os.system("open \"\" link1")

Running this results in a Windows error.

CodePudding user response:

You are currently using the variable name as a string.

Use something like this:

os.system("start \"\" " link1)

Alternatively this should also work:

os.system("start \"\" {}").format(link1)

I just tried your Darwin approach on my macOS system. Im not quite sure what you are trying to achieve with \"\". I'm sorry, if I missed something. On my system it works with open http://google.com.

  • Related