如何在 Python 中打开任何程序? [英] How to open any program in Python?

查看:82
本文介绍了如何在 Python 中打开任何程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我搜索了很多,找到了在python中打开程序的不同方法,

例如:-

导入操作系统os.startfile(path) # 在我的情况下,我必须给出一个完整的路径,这是不可能为每个程序/软件提供完整路径的.

我目前使用的第二个

导入操作系统os.system(文件名+'.exe')

在第二个示例问题中:-

  1. 如果我想打开计算器,那么它的 .exe 文件名是 calc.exe,这也会发生在任何其他程序中(而且我不知道所有 .每个程序的exe文件名).
  2. 并假设如果我将每个程序名称都硬编码,那么如果用户安装了任何新程序会怎样.(我的程序无法打开那个程序?)

如果没有其他方法可以在python中打开程序,那么是否可以获取用户计算机中所有安装程序的列表.还有 .exe 文件名(例如:- 计算器是 calc.exe 你明白了).

如果你想看看代码

注意:我想要通用解决方案.

解决方案

总有:

from subprocess import call调用([calc.exe"])

这应该允许您使用字典或列表或设置来保存您的程序名称并随意调用它们.David Cournapeau 和 chobok 的这个答案也涵盖了这一点.

Well I searched a lot and found different ways to open program in python,

For example:-

import os
os.startfile(path) # I have to give a whole path that is not possible to give a full path for every program/software in my case.

The second one that I'm currently using

import os
os.system(fileName+'.exe')

In second example problem is:-

  1. If I want to open calculator so its .exe file name is calc.exe and this happen for any other programs too (And i dont know about all the .exe file names of every program).
  2. And assume If I wrote every program name hard coded so, what if user installed any new program. (my program wont able to open that program?)

If there is no other way to open programs in python so Is that possible to get the list of all install program in user's computer. and there .exe file names (like:- calculator is calc.exe you got the point).

If you want to take a look at code

Note: I want generic solution.

解决方案

There's always:

from subprocess import call
call(["calc.exe"])

This should allow you to use a dict or list or set to hold your program names and call them at will. This is covered also in this answer by David Cournapeau and chobok.

这篇关于如何在 Python 中打开任何程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆