Drag and drop onto Python script in Windows Explorer

Question:

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?

Asked By: grok

||

Answers:

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_ROOTPython.FileshellexDropHandler]
@="{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.

Answered By: Blair Conrad

Try using py2exe. Use py2exe to convert your python script into a windows executable. You should then be able to drag and drop input files to your script in Windows Explorer. You should also be able to create a shortcut on your desktop and drop input files onto it. And if your python script can take a file list you should be able to drag and drop multiple files on your script (or shortcut).

Answered By: Evan

write a simple shell script (file.bat)

"C:python27python.exe" yourprogram.py %*

where %* stands for the all arguments you pass to the script.

Now drag & drop your target files on the file.bat icon.

Answered By: marco

Create a shortcut of the file. In case you don’t have python open .py files by default, go into the properties of the shortcut and edit the target of the shortcut to include the python version you’re using. For example:

Target: C:Python26python.exe < shortcut target path>

I’m posting this because I didn’t want to edit the Registry and the .bat workaround didn’t work for me.

Answered By: horta

With an installed python – at least 2.6.1 – you can just drag and drop any file on a python script.

import sys
droppedFile = sys.argv[1]
print droppedFile

sys.argv[0] is the script itself. sys.argv[n+1] are the files you have dropped.

Answered By: halanson

1). create shortcut of .py
2). right click -> properties
3). prefix “Target:” with “python” so it runs the .py as an argument into the python command
or
1). create a .bat
2). python some.py %*

these shortcut versions are simplest for me to do what i’m doing
otherwise i’d convert it to a .exe, but would rather just use java or c/c++

Answered By: Puddle

Late answer but none of the answers on this page worked for me.
I managed to enable/fix the drag and drop onto .py scripts using:

  1. HKEY_CLASSES_ROOT.py -> Set default value to Python.File

  2. HKEY_CLASSES_ROOTPython.FileShellOpen -> Create a key called Command with default value "C:Windowspy.exe" "%1" %*

  3. CLASSES_ROOTApplicationspy.exeopencommand -> Create keys if the don’t exist and set the default value to "C:Windowspy.exe" "%1" %*

  4. CLASSES_ROOTPython.FileShellEx -> create key DropHandler with default value {86C86720-42A0-1069-A2E8-08002B30309D}

That’s it. Test it by dragging a file onto the python script:

import sys

args = sys.argv
print(args)
Answered By: Pedro Lobito

For those who use argv in .py script but still can’t drag files to execute,
this could be solved by simply using Python Launcher (with rocket icon)

the script property "Open File" was set as python.exe,
which has no knowledge that the script needs command-line arguments "%*"

Refer to: https://bugs.python.org/issue40253

Answered By: redfishleo