How to use the Open With feature with Python?

901 Views Asked by At

I'm currently working with a python script that has the following code. It opens a file that has JSON text and determines a value from that.

browseFiles()

def browseFiles():
global fileName
fileName = filedialog.askopenfilename(title = "Select a File", filetypes = (("All Files","*.*")))

# Open the File in Read Mode
fileFile = open(fileName, "r")

# Read the file
fileContent = fileFile.read()

# Render the JSON
fileJSON = json.loads(fileContent)

# Determine the ID
myID = fileJSON["key"]

# Update the Status
windowRoot.title(myID)

... remaining code

fileFile.close()

However, it is less convenient to open the program every time, and then navigate to it.

Windows has an 'Open With' feature in File Explorer where we can right-click a file and open it with apps such as Word, etc.

How to implement this in a Python script? Should I consider creating a .exe of this script first, and if yes then which library would be most suitable for this? (Considering it is a very small and simple utility)

Some extra information that is probably unwanted: I'm using Tkinter for the GUI.

(By the way, if this question already exists on StackOverFlow or any other website, then please comment the link instead of just marking it as duplicate. I tried searching a lot and couldn't find anything)

Regards, Vivaan.

3

There are 3 best solutions below

1
On

Found another question with answers that helped me. Posting this for other people who might find this question.

answer from Roy Cai:

My approach is to use a redirect .bat file containing python someprogram.py %1. The %1 passes the file path into the python script which can be accessed with

from sys import argv
argv[1]
0
On

what you need is added new item into right click context menu.

You can take sample registry code below, modify the path to your py script C:\your_script.py and save it as anything end with .reg extension then double click to execute this registry file.

after that, you should see open with my_py when u right click on the target file

from your py script side, replace the filedialog code with fileName = sys.argv[1]

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\open with my_py\command]
@="python C:\\your_script.py %1"

enter image description here

*** Please be cautious with the registry code as wrong registry hack can be troublesome

refer this for manually modify the registry

0
On

simple example:

import sys
try:
    #if "open with" has been used
    print(sys.argv[1])
except:
    #do nothing
    pass

usage example:

import sys
from tkinter import filedialog

filetypes = (('Text files', '*.txt'),('All files', '*.*'))

#if filename is not specified, ask for a file
def openfile(filename = ''):
    #print contents of file
    if filename == '':
        filename = filedialog.askopenfilename(title='Open A File',filetypes=filetypes)
    with open(filename,'r', encoding="utf-8") as file:
        read = file.read()
    print(read)



try:
    #if "open with" has been used
    openfile(filename = sys.argv[1])
except:
    #ask for a file
    openfile()

then compile it to exe with nuitka (or whatever tool you use),
and try it.

or (for testing, without having to compile it every time you make a change):
make a .bat file

@echo off
py program.py %*
pause

Then every time you want to run it,
you open with that file.