18

I want to do the following:

  1. Save numeric data in a CSV-like formatting, with a ".foo" extension;
  2. Associate the ".foo" file extension with some python script, which in turns opens the .foo file, reads its content, and plots something with a plotting library (matplotlib most probably).

The use-case would be: double-click the file, and its respective plot pops up right away.

I wonder how I should write a python script in order to do that. Besides, the windows "open with" dialog only allows me to choose executables (*.exe). If I choose "fooOpener.py", it doesn't work.

heltonbiker
  • 26,657
  • 28
  • 137
  • 252

3 Answers3

14

This isn't really a programming question, but what you need to do is to figure out how to get the Python executable into the registry key that opens your data file.

For example, I created a little Python script called opener.py that looks like this:

import sys
print(sys.argv)
input()

Then I created a testfile.foo and used the "change" button in that file's property dialog to choose opener.py. (You can do this if you click Browse and change the Open With dialog's file filter to "All Files".)

Of course this didn't work (as you noticed). So I opened regedit and searched for opener.py and found it at the following registry key:

HKEY_CURRENT_USER\Software\Classes\Applications\opener.py\shell\open\command

The default value of this key was "C:\opener.py" %1. I changed it to python "C:\opener.py" %1. It worked!

Long story short, to do this properly you need to custom-edit the registry. Actually setting up the file association is more complex than just editing that one key (you have to also indicate that .foo is associated with opener.py).

An alternative approach would be to turn your Python script into a standalone executable using one of the several tools available for that purpose, or write a small executable in C that launches the script.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • 11
    You're setting the associations per-user (HKCU), but if you want to set this up for all users in HKLM, it's easy to use the command prompt's `assoc` and `ftype` commands, e.g. `assoc .foo=FooFile` and `ftype FooFile="C:\Python27\python.exe" "C:\opener.py" "%1" %*`. – Eryk Sun Mar 14 '15 at 16:03
  • 1
    How would I write that C program so that it passes the double-clicked file as argument to python script? – heltonbiker Dec 03 '15 at 14:10
  • @Mikhail, you need an elevated command prompt to run those commands since they're modifying the local machine registry hive. Also, this won't change an existing *user choice* in Explorer. It's only defining a ProgId that *may* be selected as a user choice in the GUI. If it's the only option Explorer will select it as the default. If you have something else selected for ".foo" files, use the Control Panel's "Default Programs" app to to select the Python opener for ".foo" files. – Eryk Sun Apr 04 '17 at 19:44
  • @eryksun thanks for the explanation. I went with this simple approach instead: http://stackoverflow.com/a/31628654/261217 – Mikhail Apr 04 '17 at 19:49
6
  1. press the windows key
  2. type cmd
  3. right click the result and choose "run as administrator"
  4. assoc .foo=foofile
  5. ftype foofile="C:\Users\<user>\AppData\Local\Programs\Python\PYTHON~1\python.exe" "C:\<whatever>\fooOpener.py" "%1" %*

Use pythonw.exe if it's a .pyw file (to prevent a cmd window from spawning).

If you want to use an existing file type, you can find its alias by not assigning anything. For example, assoc .txt returns .txt=txtfile.

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
0

instead of editing registry, you can create batch file opener.cmd

with content

python "C:\<whatever>\fooOpener.py" %1

and associate extension with this file. It works!