How can I store a link in a variable and open it using os.system()
Question:
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.
I have tried Googling this issue, but seems like no one even thought about using os.system() to open URLs.
Here is the code:
import os
import platform
link1 = "example.com"
if platform.system() == "Windows":
os.system("start "" "link1)
elif platform.system() == "Linux":
os.system("xdg-open "" "link1)
elif platform.system() == "Darwin":
os.system("open "" "link1)
Running this results in a Windows error.
Answers:
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
.
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.
I have tried Googling this issue, but seems like no one even thought about using os.system() to open URLs.
Here is the code:
import os
import platform
link1 = "example.com"
if platform.system() == "Windows":
os.system("start "" "link1)
elif platform.system() == "Linux":
os.system("xdg-open "" "link1)
elif platform.system() == "Darwin":
os.system("open "" "link1)
Running this results in a Windows error.
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
.