How to use the Open With feature with Python?
Asked Answered
A

3

0

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.

Androecium answered 17/12, 2021 at 15:16 Comment(4)
The indentation seems wrong.Catalase
It's not clear what exactly you're asking for. In the script you've provided, python itself is opening the file and reading the file's contents. Are you trying to use python to open a different program, and then use that program to open a file?Hesper
@G.Anderson Yes. The user has to open python myApp.py, which launches a Tkinter window which opens the File Explorer's Open With dialog where user selects the file, and clicking Open returns back the filename. Then, python itself reads the file and brings the content. I don't want the user to open my script and use the open with dialog. Windows has an 'Open With' feature where we right click on our file in the file explorer directly, and it lists my app as an option. So automatically the file name is passed as an argument to my script instead of the whole 'open with' thing.Androecium
Think of it exactly like how Word works. You have to open Word, go to Open Document, navigate to your document and then Open it. Then word opens the document. Which is not very convenient, right? All the people rather go to the FIle Explorer and launch the documents from there, which directly open in Word. This is basically what I'm trying to achieve here.Androecium
C
2

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.

Chanda answered 9/8, 2022 at 8:48 Comment(0)
Q
1

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

Quadratic answered 20/7, 2022 at 6:37 Comment(0)
A
0

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]
Armrest answered 20/7, 2022 at 6:13 Comment(1)
Please provide a link to the answers you are referring to.Mot

© 2022 - 2025 — McMap. All rights reserved.