Drag and drop on a Python script in Windows Explorer

I would like to drag my data file into a Python script and process the file and generate the output. The Python script accepts the data file name as a command line parameter, but Windows Explorer does not allow the script to be the target point.

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

+45
python windows drag-and-drop windows-explorer
Sep 27 '08 at 3:02
source share
6 answers

Sure. From a pointless technology article entitled “Make Python Scripts Droppable in Windows”, you can add a drop handler by adding a registry key:

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

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

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

The comment in this post indicates that you can enable the deletion of “without Python console files ( .pyw )” or “compiled Python files ( .pyc )” using the Python.NoConFile and Python.CompiledFile .

+50
Sep 27 '08 at 3:06
source share
— -

write a simple shell script (file.bat)

 "c:\Python27\python.exe" yourprogram.py %1 

where% 1 stands for the first argument you pass to the script.

Now drag and drop the% drop target files onto the file.bat icon.

+26
Apr 20 2018-12-12T00:
source share

With python installed - at least 2.6.1 - you can simply drag and drop any file into a python script.

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

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

+6
Aug 10 '17 at 15:04 on
source share

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

+5
Dec 20 2018-10-12T00:
source share

Create a file shortcut. If you do not have ppyon open.py files by default, go to the shortcut properties and edit the shortcut target to include the version of python that you are using. For example:

Target: C: \ Python26 \ python.exe <target path shortcut>

I am posting this because I do not want to edit the registry, and the .bat workaround does not work for me.

+2
Feb 17 '14 at 10:23
source share

one). create shortcut .py
2). right click -> properties
3). prefix "Target:" with "python", so it runs .py as an argument to the python command
or
one). create.bat
2). python some.py% *

these shortest versions for me the easiest thing to do is what I do
otherwise I would convert it to.exe, but rather just use java or c / c ++

0
Dec 03 '18 at 2:29
source share



All Articles