简体   繁体   English

如何将链接存储在变量中并使用 os.system() 打开它

[英]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.我想在 Python 中制作一个应用程序来打开用户分配给按钮的链接。 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.我尝试用 webbrowser 模块打开它们,但它们会在 Microsoft Edge 中打开,而不是在默认浏览器中打开,所以我决定使用 os.system(),但我似乎无法成功打开变量中的链接。

I have tried Googling this issue, but seems like no one even thought about using os.system() to open URLs.我试过谷歌搜索这个问题,但似乎没有人想过使用 os.system() 打开 URL。

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.运行此命令会导致 Windows 错误。

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.我刚刚在我的 macOS 系统上尝试了你的 Darwin 方法。 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 .在我的系统上,它适用于open http://google.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM