Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I directly open a custom file with python on a double click?

I am programming on a windows machine and I have an app that reads file selected by the user. Is it possible to allow them to open the file directly when they double click. This needs to work when the program is "compiled" as an .exe with cxfreeze.

What I am really asking is this: Is there a way to allow the user to double click on a custom file (.lpd) and when they do windows starts the program (a compiled cxfreeze .exe) and passes it the file path as an argument.

like image 523
Vogon Jeltz Avatar asked Nov 01 '22 03:11

Vogon Jeltz


2 Answers

The only way Windows associates files with a particular program is by their extension, so this only works if your files have a unique extension (which it looks like maybe they do). So your user would need to setup the association on their machine, which varies depending on the version of Windows. For instance, in Windows 7 it would probably be through Control Panel\All Control Panel Items\Default Programs\Set Associations.

It is possible for you to automatically setup this association on their system (probably by editing the Windows registry), but that would generally be done during an installation, and you should ask the users permission to do this first.

like image 198
brianmearns Avatar answered Nov 05 '22 03:11

brianmearns


I used PyInstaller for exe-generation. Here is a small example:

import sys

class Test():
    def __init__(self, path=None):
        super().__init__()
        self.path = path

    def start(self):
        if self.path == None:
            pass
        else:
            print(self.path)


if __name__ == '__main__':
    if len(sys.argv) > 1 :
        mytest = Test(sys.argv[1])
    else:
        mytest = Test()

    mytest.start()
like image 26
picibucor Avatar answered Nov 05 '22 03:11

picibucor