在Python中获取文件关联 [英] Get file association in Python

查看:118
本文介绍了在Python中获取文件关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Linux和Windows上都得到与扩展相关的程序。我知道我可以在Linux中执行xdg-open,也可以在Windows中启动,但是我将在远程服务器上打开一个媒体文件,所以使用这种方法将会打开浏览器,而不是默认的媒体播放器。如何获得扩展的默认应用程序?

解决方案

有一个名为 xdg-mime ,就像xdg-open一样,但打印信息而不是试图打开文件。 / p>

  xdg-mime查询文件类型/tmp/foobar.png 



打印文件/tmp/foobar.png的MIME类型,在本例中为image / png

  xdg-mime查询默认图片/ png 

打印.desktop文件名在Windows上,如果找不到API,也许可以使用 assoc code>或 ftype ,它们是 cmd 的一部分。


I want to get the program associated with an extension, in both Linux and Windows. I know that I can execute xdg-open in Linux or start in Windows, but I'm gonna open a media file on a remote server, so using this method would open the web browser, not the default media player. How can I get the default application for an extension?

解决方案

There is a program called xdg-mime that's like xdg-open, but prints the information rather than trying to open the file.

xdg-mime query filetype /tmp/foobar.png

Prints the MIME type of the file /tmp/foobar.png, in this case image/png

xdg-mime query default image/png

Prints the .desktop filename of the application which is registered to open PNG files.

On Windows, if you can't find an API, maybe you can use assoc or ftype, which are part of cmd.

这篇关于在Python中获取文件关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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