Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading