简体   繁体   English

使用 python 打开其他应用程序

[英]Opening other applications using python

I am using Ubuntu 12.04 on a 64bit laptop.我在 64 位笔记本电脑上使用 Ubuntu 12.04。 I am trying to open an application using python code.我正在尝试使用 python 代码打开一个应用程序。

import os
os.system("open /home/utsav/ab.txt")

It gives the following error:它给出了以下错误:

"Couldn't get a file descriptor referring to the console 256" “无法获得引用控制台 256 的文件描述符”

What command do I want to use?我想使用什么命令?

The command you're using - open is actually another command referred to in the man pages as openvt , which opens a virtual terminal.您正在使用的命令 - open实际上是手册页中称为openvt另一个命令,它打开一个虚拟终端。

I don't think that's what you want to do, so you would want to use another command (such as gnome-open, xdg-open, geany, gedit, vim, or nano).我认为这不是您想要做的,因此您可能想要使用另一个命令(例如 gnome-open、xdg-open、geany、gedit、vim 或 nano)。

I tried this one.我试过这个。 it works for me.这个对我有用。

import os
os.system("command of your preferred app")

the command for app can be found from Desktop Entry tab of Properties of the preferred app.可以从首选应用程序属性桌面条目选项卡中找到应用程序的命令。

open works on OS-X, under ubuntu I end up using gnome-open (I don't know what the corresponding command is if you're using the k-desktop). open适用于 OS-X,在 ubuntu 下我最终使用gnome-open (如果您使用的是 k-desktop,我不知道相应的命令是什么)。

EDIT编辑

based on the comment by Niklas B., you could probably also try xdg-open .根据 Niklas B. 的评论,您也可以尝试xdg-open

For what it's worth, the Python 2.7.3 documentation says that the os module is deprecated and the subprocess module should be used instead.就其价值而言,Python 2.7.3 文档说os模块已弃用,应改用subprocess模块。 To execute a command this way you can use subprocess.call(args, ...) ( "Using the subprocess module" ).要以这种方式执行命令,您可以使用subprocess.call(args, ...)“使用子流程模块” )。

Based on the earlier answers you can use open for Mac OS X and gnome-open for a Linux distro running the Gnome desktop environment.根据之前的答案,您可以将open用于 Mac OS X,将gnome-open用于运行 Gnome 桌面环境的 Linux 发行版。 (I checked gnome-open and xdg-open and both work on Fedora 16.) Windows is a bit tricky. (我检查了gnome-openxdg-open ,它们都在 Fedora 16 上工作。)Windows 有点棘手。

For Windows you need to use start , but if there are spaces in the path to the file or the filename, then it doesn't work right.对于 Windows,您需要使用start ,但如果文件路径或文件名中有空格,则它无法正常工作。 Quoting the filename doesn't quite fix it, either, since start expects an unmarked argument with quotes around it to be a title (eg for a new cmd interface window).引用文件名也不能完全解决它,因为start期望一个带有引号的未标记参数作为标题(例如,对于新的cmd界面窗口)。 This is a problem since neither the title nor the file to open is a marked argument, so to get the call to work right you have to do something like start "DummyTitle" "Filename with spaces.ext" .这是一个问题,因为标题和要打开的文件都不是标记参数,因此start "DummyTitle" "Filename with spaces.ext"调用正常工作,您必须执行诸如start "DummyTitle" "Filename with spaces.ext"

So what we've got is:所以我们得到的是:

  • Mac OS X: subprocess.call(['open','/path/to/file']) Mac OS X: subprocess.call(['open','/path/to/file'])

  • Linux running Gnome DE: subprocess.call(['gnome-open','/path/to/file']) Linux 运行 Gnome DE: subprocess.call(['gnome-open','/path/to/file'])

  • Windows: subprocess.call(['start','"DummyTitle"','"C:\\\\Path\\\\to\\\\File.ext"']) Windows: subprocess.call(['start','"DummyTitle"','"C:\\\\Path\\\\to\\\\File.ext"'])

Or something like that.或类似的东西。


For versions of Python before 2.7.3, you could us the os module with these args as strings as you suggest in your question (just altering the command portion of the system call).对于 2.7.3 之前的 Python 版本,您可以使用os模块将这些 args 作为字符串,如您在问题中所建议的那样(只需更改系统调用的命令部分)。 Note as well that there's os.startfile('/path/to/file') for Windows which will open the file with whatever the associated default is for that filetype, if you are going the deprecated os route.还要注意,Windows 的os.startfile('/path/to/file')将使用该文件类型的关联默认值打开文件,如果您要使用已弃用的os路由。


Note that we still don't have a confirmed way of opening files on OSes using other desktop environments.请注意,我们仍然没有确定的方法可以在使用其他桌面环境的操作系统上打开文件。 Please suggest improvements to this answer!请对此答案提出改进建议!

Welcome to 2022!欢迎来到 2022!

You can simply make use of PYPI module AppOpener .您可以简单地使用 PYPI 模块AppOpener

# pip install AppOpener
from AppOpener import run
run("whatsapp") #Opens whatsapp if installed
run("whatsapp, telegram") # Opens whatsapp & telegram

Visit AppOpener's documentation here .在此处访问 AppOpener 的文档

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

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