vy32
vy32

Reputation: 29655

Set Windows file type association for a specific file only

How do I programmatically associate a file with a specific "opens with" action? That is, I am creating filename.py on a Windows computer, putting it on the desktop. I want double-clicking it to run the python intrepreter, rather than opening the text editor.

I can do this with the Properties window, but I want to do this automatically, when I create it (from another python program).

I do not want to set up a universal file extension, because I want double-clicking on .py files to result in bringing up the editor, not running the program. It is only for this particular file that I want that behavior. I can do this from the Windows GUI; I want to know how to do it programmatically.

There is no installer. The Python .py program is being created from another python program. It is being created directly in the file system with a open("filename.py").write(file_contents).

The environment is Windows 7

Upvotes: 1

Views: 966

Answers (1)

ivan_pozdeev
ivan_pozdeev

Reputation: 36036

In Windows, file type associatons are per-extension. Any file with the same extension is treated the same. That's the whole idea behind them.

In principle, you can change the .py type association to run some program that would check if it runs this particular file and do a different thing.

A much easier, more manageable and predictable way to go would be to place a shortcut rather than the program onto the desktop that would run anything you need -- in this case, "path\to\python.exe" "path\to\your_program.py". That's the whole idea behind shortcuts btw. The file itself can be placed to some other user-writable place like %APPDATA%\<your_project_name> which is the standard location for application data.

Upvotes: 1

Related Questions