Why after pressing context menu button from python code below doesn't receive any…
- User
Why after pressing context menu button from python code below doesn't receive any sys.argv?
import winreg, sys
key_path = r"*\shell\Avifify" # Add entry for `SUPPORTED_EXTS` and for multi selection for ext in SUPPORTED_EXTS: # Use system file associations to specify the extension context key_path = r"SystemFileAssociations\." + ext.lower() + r"\shell\Avifify" try: # Create the main context menu key for the extension. with winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, key_path) as key: winreg.SetValueEx(key, None, 0, winreg.REG_SZ, "Avifify") # Enable multi-selection support. winreg.SetValueEx(key, "MultiSelectModel", 0, winreg.REG_SZ, "Player") # Create the 'command' subkey to define the action. with winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, key_path + r"\command") as cmd_key: command = f'"{sys.executable}" "{os.path.abspath(__file__)}" %*' winreg.SetValueEx(cmd_key, None, 0, winreg.REG_SZ, command) print(f"Context menu entry added for .{ext.lower()} files successfully.") except Exception as e: print(f"Failed to add context menu entry for .{ext.lower()}: {e}") Assistant
The issue is likely with how you're handling the command line arguments and the
%*parameter expansion. Here are the most common causes and solutions:1. Missing
osimportYour code uses
os.path.abspath(__file__)but doesn't importos. Add this at the top:import os2. Command string formatting
The
%*parameter passes selected files as arguments, but there might be issues with how Windows expands it. Try this more explicit approach:command = f'"{sys.executable}" "{os.path.abspath(__file__)}" "%1"'Or for multiple file support:
command = f'"{sys.executable}" "{os.path.abspath(__file__)}" %*'3. Debug the arguments
Add debugging to see what's actually being passed:
import sys import os print(f"Number of arguments: {len(sys.argv)}") print(f"Arguments: {sys.argv}") # Your main code here if len(sys.argv) > 1: files = sys.argv[1:] print(f"Files to process: {files}") else: print("No files provided")4. Registry permissions
Make sure your script runs with administrator privileges when creating the registry entries, as modifying
HKEY_CLASSES_ROOTrequires elevated permissions.5. Alternative approach for multiple files
If you're still having issues with
%*, try using%1and handle multiple selections differently:# In registry command = f'"{sys.executable}" "{os.path.abspath(__file__)}" "%1"' # In your Python script if len(sys.argv) > 1: selected_file = sys.argv[1] # Process the fileThe most likely issue is the missing
osimport or registry permissions. Try adding the import and running your registry modification code as administrator.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.