1

I have developed a .txt file opener application in Tkinter, python. I created the code to .exe and then I also made a setup.exe with InnoSetup. So now I want that all of the .txt files should be opened with my application.

As far as my coding skill goes on, I think this is not something to handle with Python. I think some PowerShell command or InnoSetup may be able to handle it.

So I have an open function in the application. Something like this:

def open(filepath):
    bla bla bla

After some time, I got into my head how it will use my open function situated in my code. Can anyone help me to figure out how I can associate my application with all .txt files?

Please comment if there is a lack of information in this question. I will certainly clarify it.

Sabil Islam
  • 106
  • 5

1 Answers1

0

This needs some workaround with the Windows Registry. This question's answers may be helpful: Associate file extension to python script, so that I can open the file by double click, in windows.

In your case, since it's an EXE, you might need to change the command a bit.

Firstly, run cmd (command prompt) as an administrator. You can do that by searching for cmd and right clicking it to click the "Run as administrator" option. The path can be just "C:\WINDOWS\system32>".

I tried changing the "%*", removing it and finally, I got a command which worked! I tried the following command and it works fine for EXEs:

ftype txtfile=C:\Users\<Your name goes here>\main.exe %1

Or wherever your EXE is located (Copy-paste the path with the filename).

The syntax for EXEs is ftype txtfile=<path>\<filename>.exe %1.

Now, you should have the default app that opens .txt files changed!

The name "txtfile" can be changed using assoc .foo = foofile You can find more about it in Honest Abe's answer.

You can do the same with the other registered file types also, I guess.

Since you want the setup to be distributed, I think changing something in the python file could help, since it needs the admin powers... Maybe something like:

import win32com.shell.shell as shell
commands = 'ftype txtfile=C:\Users\<Your name goes here>\main.exe %1'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)

This question provides more information on how to run command prompt as an administrator: How to run cmd command in Python with admin.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
The Amateur Coder
  • 789
  • 3
  • 11
  • 33