views:

1986

answers:

3

I would like to drag and drop my data file onto a Python script and have it process the file and generate output. The Python script accepts the name of the data file as a command-line parameter, but Windows Explorer doesn't allow the script to be a drop target.

Is there some kind of configuration that needs to be done somewhere for this work?

+15  A: 

Sure. From a mindless technology article called "Make Python Scripts Droppable in Windows", you can add a drop handler by adding a registry key:

Here’s a registry import file that you can use to do this. Copy the following into a .reg file and run it (Make sure that your .py extensions are mapped to Python.File).

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

This makes Python scripts use the WSH drop handler, which is compatible with long filenames. To use the short filename handler, replace the GUID with 86C86720-42A0-1069-A2E8-08002B30309D.

A comment in that post indicates that one can enable dropping on "no console Python files (.pyw)" or "compiled Python files (.pyc)" by using the Python.NoConFile and Python.CompiledFile classes.

Blair Conrad
A: 

Where do you drop the file? Into the console window? Or onto the script file icon? I'm not following.

Greg
On the script file icon.
Blair Conrad
A: 

You drop the file's icon onto the script's Icon. However when I do that I get an index out of range error. Because of course the script is an argument for python and the file you are dropping on the script is an argument for your script. No wonder the computer gets confused. I added file to the registry but I still get the same error. I'm able to drop files onto the scripts icon and the script runs but it errors out because the index is out of range so it can't get the name of the file dropped on it.

Mark