开发者

Get file association in Python

开发者 https://www.devze.com 2023-02-22 01:52 出处:网络
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,

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 extens开发者_如何学运维ion?


Covering Windows only below:

I think it'll be close to what I just went through for .doc (associated with winword.exe).

On my machines...

STEP 1: Take a look at the registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.doc


On Windows XP

STEP 2: Look at the default value (ignore everything else there). The default value I'm seeing is Word.Document.8

STEP 3: Now use that, and insert it (notice it in the key name below), to read the default value of

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.8\shell\Open\command

That value is: "\"C:\Program Files\Microsoft Office XP Pro 2003\OFFICE11\WINWORD.EXE\" /n /dde"

And that is the path to MS Word on my system, at least on XP.

Also just noticed, about the suggestions by Mikel above, those nail it on XP:

C:\>assoc .doc
.doc=Word.Document.8

C:\>ftype Word.Document.8
Word.Document.8="C:\Program Files\Microsoft Office XP Pro 2003\OFFICE11\WINWORD.EXE" /n /dde

On Windows 7

The default from STEP 1 has no value set, darn, there went the simplicity. There is a subkey called 'OpenWithProgIDs', so I guess you would look for that if the default is empty or depending on the OS version.

STEP 2: Read the subkey called 'OpenWithProgIDs'. There is an item named Word.Document.8, so we're back to that now.

STEP 3: Again plug that in, to read the default value of

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.8\shell\Open\command

In this case (mine) the value is more complex:

"C:\Program Files (x86)\Common Files\microsoft shared\virtualization handler\cvh.exe" "Microsoft Word Starter 2010 9014006604090000" /n "%1"

In the right neighborhood, as far as using the registry.

Unfortunately on Windows 7, 'assoc' and 'ftype' didn't do the trick at least for .doc and the path to winword.exe on my machine.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消