59

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
grok
  • 906
  • 1
  • 14
  • 14
  • i think it just works if you installed python from the windows setup installer (basically if you can double click to run a python script) – Joran Beasley Sep 13 '13 at 18:57
  • On Windows 10, I had to double click on the PY file and open with Python once in order for the drop handler to be recognized. – XP1 Jul 10 '20 at 05:17

8 Answers8

61

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
  • 233,004
  • 25
  • 132
  • 111
  • Where do you drop the file? Into the console window? Or onto the script file icon? I'm not following. – Greg Sep 27 '08 at 17:32
  • Thank you @Blair for your solution. I'm a beginner and I get the error **No such file or directory** after dropping. You can fix this by adding the following at the beginning of the script : `exepath = sys.argv[0] if '\\' in exepath: os.chdir(exepath[:exepath.rfind('\\')])` – Bludwarf Oct 22 '14 at 16:59
  • 4
    This information is outdated. The WSH drop handler was added to Python's installer in 2.6.1 (2008-12). However, a new drop handler was created for 3.6.0 (2016-12) and 3.5.3 (2017-01) because the WSH handler doesn't support Unicode paths. As long as .py files are associated with the `Python.File` progid, this should just work for all versions of CPython that are currently supported on Windows. But it's a good idea to install 3.5.3+ to get the new drop handler. – Eryk Sun Jun 03 '17 at 08:32
  • @Norfeldt I have a clunky error printing thing at https://github.com/endolith/waveform_analysis/blob/master/scripts/wave_analyzer_launcher.py – endolith Aug 22 '17 at 01:52
  • 1
    @eryksun Can you add this information to [halanson's answer](https://stackoverflow.com/a/45617351/3357935)? – Stevoisiak Sep 19 '18 at 16:44
  • 2
    Can anyone clarify what is meant by "Make sure that your .py extensions are mapped to Python.File"? I have .py files set to open with python.exe by default, but this reg script isn't allowing me to drag drop on them. – pyjamas Jan 25 '19 at 00:11
44

write a simple shell script (file.bat)

"C:\python27\python.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.

Victor Uriarte
  • 479
  • 4
  • 9
marco
  • 441
  • 4
  • 2
18

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.

halanson
  • 419
  • 7
  • 15
  • This answer is the simplest and easiest. Also, tested to work with py2exe (using Python 3.3) after it has been converted to an exe. – Splic Jan 11 '18 at 21:45
  • This works, but like other solutions, it seems like the files are in a random order. Any way to preserve the order in which they were selected? – Gillespie Jul 11 '20 at 04:23
  • @Gillespie not really. Try to click and drop the first file in your selection instead of some file in the mid-selection. Depending on your use case you could also use an internal sorting algorithm. – halanson Jul 11 '20 at 10:13
6

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).

Evan
  • 61
  • 1
  • 1
3

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:\Python26\python.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.

horta
  • 1,110
  • 8
  • 17
  • while i didn't follow this perfectly, this answer did expose my issue of my target of my python script was set by default to "open with" VS code instead of python. changing it to python fixed my issue – simon Nov 08 '22 at 15:27
3

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++

Puddle
  • 2,993
  • 1
  • 19
  • 32
2

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_ROOT\Python.File\Shell\Open -> Create a key called Command with default value "C:\Windows\py.exe" "%1" %*

  3. CLASSES_ROOT\Applications\py.exe\open\command -> Create keys if the don't exist and set the default value to "C:\Windows\py.exe" "%1" %*

  4. CLASSES_ROOT\Python.File\ShellEx -> 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)
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • This worked for me, although remember to terminate explorer.exe and start it again. Also, not sure if it's normal but it ends up with a truncated filename like 829897~1.JSO instead of the real `82989762511648_11612593811973.json`, but trying to open the path works as expected. – Ehsan Kia Apr 20 '21 at 04:59
  • 1
    Found the solution to my own problem, instead of {86C... as suggested in Step 4, instead use {60254CA5-953B-11CF-8C96-00AA00B8708C} to get the better DropHandler which supports long names. – Ehsan Kia Apr 20 '21 at 05:27
0

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

redfishleo
  • 21
  • 3