如何在 Python 中切换执行到新脚本?

发布于 2024-12-21 18:11:07 字数 270 浏览 2 评论 0原文

是否可以让一个脚本调用另一个脚本,然后退出(不知道我是否正确),让另一个脚本运行?具体来说,我想要一个更新脚本来下载 installer.py 然后运行它。由于 installer.py 覆盖了更新脚本,我不能只执行 subprocess.call() ,因为它会在“删除旧安装”阶段失败。

这是可能的还是我必须单独保留更新程序脚本,替换其他所有内容,将新脚本放入临时目录中,然后在下次运行程序时替换它?这被认为是更好的方法吗?

非常感谢你,如果这太明显了,我很抱歉。

Is it possible to have one script call another and then exit (don't know if I'm putting this right), leaving the other script running? Specifically I want an update script to download installer.py and then run it. Since installer.py overwrites the update script I can't just do a subprocess.call() since it will fail at the 'delete old install' stage.

Is this possible or must I instead leave the updater script alone, replace everything else, put the new one in a temporary directory and then replace it next time the program is run? Is this considered a better approach?

Thank you very much and sorry if this is blindingly obvious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮颜 2024-12-28 18:11:07

os.exec* 系列之一函数应该做你想做的事。

One of the os.exec* family of functions should do what you want.

寂寞花火° 2024-12-28 18:11:07

由于您正在下载另一个 Python 脚本,因此您可以尝试使用 exec 来运行它。在 2.x 中,有一个辅助函数 execfile 用于此类事情(它包装了 exec 关键字的使用;在 3.x 中,您必须读取该文件并通过exec 的结果字符串(如果我没读错的话,它现在是一个函数)

在这种情况下,您必须始终 110% 确定您信任该内容!< /strong> 看来这不是问题不过,你。

Since you're downloading another Python script, you could try using exec to run it. In 2.x there is a helper function execfile for this sort of thing (which wraps the use of the exec keyword; in 3.x you must read the file and pass the resulting string to exec (which is now a function, if I'm reading correctly).

You must always be 110% sure you trust the content in cases like this! It seems that this isn't an issue for you, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文