2

In Windows 10, is it possible to make a file with a specific extension run with a given Python script? For example, I have .pickle files saved, and I wrote a simple Python script to print them out. I would like to double click on the .pickle file and have the Python script that prints it run.

I was able to create a context menu entry that successfully did this. For example, this simple Python script named view_pickle_file.py:

import pickle
import sys

filename = sys.argv[1]
print(filename)

var = pickle.load(open(filename, 'rb'))

print(type(var))
if type(var) == list:
  print(len(var))
  for ii, value in enumerate(var):
    print(ii, value)
elif type(var) == dict:
  print(len(var))
  for key, val in var.items():
    print(key, val, sep="\t")
elif type(var) == set:
  print(len(var))
  for ii in var:
    print(ii)
else:
  print(var)

input("Press enter")

And the registry key at Computer\HKEY_CLASSES_ROOT\SystemFileAssociations\.pickle\shell\View\command with a (Default) REG_SZ Data value of "C:\Program Files\Python\Python38\python.exe" "C:\Scripts\Python\view_pickle_file.py" "%1":

creates:

enter image description here

which prints out the pickle file perfectly.

But this is a context menu entry. I want to double click the .pickle file to make this happen. How would I do that? Is it possible? Or is the context menu entry as close as I will ever get?

4
  • Try to double the back-slashes in the registry like this : `\\`.
    – harrymc
    Sep 28 '20 at 18:38
  • 1
    @harrymc, I'm not having any issues with the example I showed (the view context menu entry works correctly). The problem is that I don't know how to achieve the same thing by double clicking the file. Is there some registry manipulation I can do to achieve that?
    – jippyjoe4
    Sep 28 '20 at 20:14
  • Does your entry show up in (1) Context menu "Open with", and (2) Default Programs > Associate a file type or protocol with a program > Choose default applications by f‌ile type > .pickle ?
    – harrymc
    Sep 29 '20 at 6:41
  • @harrymc, no it does not. I assume I will need to add some different registry key to make it work by double clicking.
    – jippyjoe4
    Sep 29 '20 at 14:21
1
+50

Try this registry update:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.pickle]
@="python.pickle"

[HKEY_CLASSES_ROOT\python.pickle]
@="Pickle Document"

[HKEY_CLASSES_ROOT\python.pickle\shell]

[HKEY_CLASSES_ROOT\python.pickle\shell\open]

[HKEY_CLASSES_ROOT\python.pickle\shell\open\command]
@="\"C:\\Program Files\\Python\\Python38\\python.exe\" \"C:\\Scripts\\Python\\view_pickle_file.py\" \"%1\""
2
  • Excellent! This worked great. Can you explain why the last key isn't the only one required? What's the point of the first two keys instead of just using "[HKEY_CLASSES_ROOT\pickle\shell\open\command]" for the last one?
    – jippyjoe4
    Sep 30 '20 at 14:14
  • The purpose is to set the display name for "Open with".
    – harrymc
    Sep 30 '20 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.