Drag and drop onto Python script in Windows Explorer

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: Carlos195 | Posted: 01-10-2021






Answer 1

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.

Answered by: Julia166 | Posted: 02-11-2021



Answer 2

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.

Answered by: Arthur179 | Posted: 02-11-2021



Answer 3

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: Walter684 | Posted: 02-11-2021



Answer 4

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: Sarah112 | Posted: 02-11-2021



Answer 5

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: Victoria589 | Posted: 02-11-2021



Answer 6

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.

Answered by: Clark524 | Posted: 02-11-2021



Answer 7

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)

Answered by: David139 | Posted: 02-11-2021



Answer 8

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: Brad362 | Posted: 02-11-2021



Similar questions

python - Can't see folder in windows explorer and can't rename

I'm new to programming, python, and StakeOverflow in general ( I had one programming course in college and one in high school). I made this script to cleanout out my downloads folder but I am running into some issues. The folder I create is not visible in windows explorer and I can't seem to rename the I created folder. I'm running windows 10 if that makes any difference. import os import time import shuti...


python - How to Detect when a Windows Explorer Window has been closed?

Using python, I need to be able to detect when a windows explorer window has been closed by the user. At first I tried PID's before i realised that it was all connected to a central proccess, "explorer.exe" :( classic noobish me (this meant that PID wasn't changed at all after it was closed). So my question is, how would i be able to detect when a windows explorer windows has been closed. Thank you :) Here is my cu...


Use Windows explorer to get the path to a file in python

How would I 1. Access windows explorer 2. Use it to to gain the path to a file. For example when I click "open" it pastes the address of the selected file into an entry box I'm using tkinter if that helps at all. Thanks in advance.


macos - Python script that can run from OSX Finder and Windows Explorer

I have a bunch of useful Python3 scripts that I use in different OS environments. They run fine when called from the terminal but I want to run them from both Windows Explorer and OSX Finder. In Windows, that is not an issue as the *.py files get associated with the installed interpreter. For OSX, I am having some problems. I tried using the shebang line. #!/usr/bin/python3 But Fin...


how to close file explorer in Windows using python

I'm very new to python and I am trying to write a short script that simply opens file explorer then closes it again, I can get it to open fine using the following.. import os import subprocess os.startfile("Y:\\") I tried adding the following to then close the explorer window but this seems to do nothing os.system("taskkill /f /im explorer.exe") I'm sure I...


Python file explorer in windows 7

I just want to get a way to open and close any particular folder through python code in windows 7 and above, Any Suggestion will be a great help. Thank you. this is to open the directory task = subprocess.Popen('explorer "C:\\', shell=True) p = task.pid this is to close through the PID os.popen('TASKKILL /PID ' + str(p) + ' /F') The problem is with...


python - How to type into a text field in Windows Explorer?

I am trying to upload a file to a website, and when I click on the upload button (using module WebBot) it opens Windows Explorer. Am I able to output the name of the file into the File Name field? I have the full path of the file, I just need to get the actual text into the File Name box.


python - How to open windows explorer at 'This PC'?

How can I open Windows Explorer at 'This PC', using python 3.6 when using Windows 10? I am not sure what the path is to 'This PC'. (Previously 'My Computer'). I am able to open Windows Explorer to known paths using: import os os.startfile(os.path.realpath(DIRECTORY_PATH))


Python Web App to Open Windows Explorer through Heroku server

This question already has answers here:


python - Launch Spyder from Windows Explorer

I want to know how to launch Spyder for my .py files. Currently, I have to go all over anaconda navigator and select spyder to launch so I made a shortcut to spyder in the Start Menu. I have tried the Open with Path\anaconda3\Scripts\spyder.exe methods but just shows a command prompt for half a second and then exits. While browsing I saw something about the envs folder in anaconda...


python - PyQt drag and drop outside app into Windows file explorer?

I would like to create files by dragging from a QListWidget into the OS file explorer (Windows 10 in my case), is this possible? So in the widget below, I want dragging &quot;one&quot; and &quot;two&quot; from the list onto a folder in the system file explorer to create two files named &quot;one.txt&quot; and &quot;two.txt&quot; containing the text &quot;one&quot; and &quot;two&quot; respectively import sys ...


python - How to make a Windows Explorer Window appear in a kivy app?

I am making an app in python alongside kvlang and I want to have a popup windows explorer window appear for user to select a path. Kind of like in Discord when you're sending someone a file: Now I haven't found a way online to do this which makes me won...


How can set Windows Explorer startup path with python

I want to set Windows Explorer startup path with python. like this. %windir%\explorer.exe C:\Users.This is a global setting. it’s need pywin32 or cmd or regedit? please help me。 thanks


python - Can't see folder in windows explorer and can't rename

I'm new to programming, python, and StakeOverflow in general ( I had one programming course in college and one in high school). I made this script to cleanout out my downloads folder but I am running into some issues. The folder I create is not visible in windows explorer and I can't seem to rename the I created folder. I'm running windows 10 if that makes any difference. import os import time import shuti...


python - How to Detect when a Windows Explorer Window has been closed?

Using python, I need to be able to detect when a windows explorer window has been closed by the user. At first I tried PID's before i realised that it was all connected to a central proccess, "explorer.exe" :( classic noobish me (this meant that PID wasn't changed at all after it was closed). So my question is, how would i be able to detect when a windows explorer windows has been closed. Thank you :) Here is my cu...


Use Windows explorer to get the path to a file in python

How would I 1. Access windows explorer 2. Use it to to gain the path to a file. For example when I click "open" it pastes the address of the selected file into an entry box I'm using tkinter if that helps at all. Thanks in advance.


macos - Python script that can run from OSX Finder and Windows Explorer

I have a bunch of useful Python3 scripts that I use in different OS environments. They run fine when called from the terminal but I want to run them from both Windows Explorer and OSX Finder. In Windows, that is not an issue as the *.py files get associated with the installed interpreter. For OSX, I am having some problems. I tried using the shebang line. #!/usr/bin/python3 But Fin...


how to open windows explorer with search result from python

I have created a File indexer and searcher script in python which indexes all files which could be used later for fast search . I want to open the search result in windows explorer . I know subprocess.Popen(r'explorer /select,"C:\path\of\folder\file") this command execution will open a specified directory . I have also read


how to close file explorer in Windows using python

I'm very new to python and I am trying to write a short script that simply opens file explorer then closes it again, I can get it to open fine using the following.. import os import subprocess os.startfile("Y:\\") I tried adding the following to then close the explorer window but this seems to do nothing os.system("taskkill /f /im explorer.exe") I'm sure I...


Python file explorer in windows 7

I just want to get a way to open and close any particular folder through python code in windows 7 and above, Any Suggestion will be a great help. Thank you. this is to open the directory task = subprocess.Popen('explorer "C:\\', shell=True) p = task.pid this is to close through the PID os.popen('TASKKILL /PID ' + str(p) + ' /F') The problem is with...


python - How to type into a text field in Windows Explorer?

I am trying to upload a file to a website, and when I click on the upload button (using module WebBot) it opens Windows Explorer. Am I able to output the name of the file into the File Name field? I have the full path of the file, I just need to get the actual text into the File Name box.


python - How to open windows explorer at 'This PC'?

How can I open Windows Explorer at 'This PC', using python 3.6 when using Windows 10? I am not sure what the path is to 'This PC'. (Previously 'My Computer'). I am able to open Windows Explorer to known paths using: import os os.startfile(os.path.realpath(DIRECTORY_PATH))


python - Open explorer on a file

In Python, how do I jump to a file in the Windows Explorer? I found a solution for jumping to folders: import subprocess subprocess.Popen('explorer "C:\path\of\folder"') but I have no solution for files.


python - open file in finder or explorer on linux or unix

I don't have any Mac OSx or Linux machine, but I want to implement the functionality like opening an explorer from a selected file path. for example import subprocess subprocess.Popen('explorer "E://temp//"') the above code opens windows explorer for a specified path, how to do it for Mac or Linux ?


Python (2.6) ftplib didn't connect to site, but FTP worked from IE Explorer

I'm not certain if I'm misunderstanding something about FTP, or if there is something funny about the site I was trying to FTP into. When I tried: import ftplib HOST = 'www.site.org' USER = &lt;user name&gt; PASSWD = &lt;password&gt; FTPConn = ftplib.FTP(HOST,USER,PASSWD) the result was: error:[Errno 10060] A connection attempt failed because the connected party did...


explorer - Open a file browser in a specific folder with Python

I'm a french guy, so excuse me if my English is not very good. Here is my problem : I'm using python to make a script that works with Blender, and I need to import an .obj file. I already have the importer, but I enter the entire filepath. It's look like that : bpy.ops.import_scene.obj(filepath='C:/Users/MarcPro/Desktop/testauto/03-Reconstruction/Data/Tile/Tile.obj', axis_forward='Y', axis_up='Z') ...


eclipse - When I use Mylyn with PyDev I see the context in the task but python files are not shown in any explorer

I use mylyn 3.9.0 with PyDev 2.7.5 with the PyDev Mylyn integration 0.4.0. Mylyn seems to build up contexts correctly (I can see the context tree in the task/context tab). But the python files are not shown "PyDev Package Explorer" nor in the "Project Explorer". What could prevent the python files to appear? Uninstalling the PyDev Mylyn integration did not help.


python - Pydev Package Explorer Issues

I have just installed Pydev on my existing copy of Eclipse for Java, running on OSX 10.7. When I make a new pydev package, I click away from the default directory for my java files and into one for python files. Once it is created, the project shows in the pydev package explorer window. However, once I close the package, it disappears from the pydev explorer window, but shows in the java explorer window (under java pers...


Open windows explorer at a specific network location in Python

I have tried so many variants of a theme to get this explorer window open at the P:\ drive, from what my little knowledge tells me, the fact the path to the folder is anywhere but the C:\ drive means it fails (it works with C:) so perhaps the path is wrong? the code below shows some of the tries i have made but still no luck, "P:" is mapped the same on all machines. def Open_Win_Explorer_and_Select_Dir(): ...


In python is there a way to use the windows explorer to create a file path which can then be returned as a string?

I am making a program to manage a database and i need a nice way of choosing the save locations of files within the program. I was wondering if it is possible to open windows explore from my program, select a folder to save a file in, enter the file name and return the file path as a string to the main program.


How to get python to read a file that has been opened through File Explorer?

I don't exactly know how to phrase this question, but I have tried my best. Let's say I have an application and I have set it to be the default to run when the .example file extention has been double clicked within File Explorer. Now, how do I get this application not to just launch, but instead to react as if it has been asked to open the file. Now I know there is the: file = open ("C:\\Ex...


How can set Windows Explorer startup path with python

I want to set Windows Explorer startup path with python. like this. %windir%\explorer.exe C:\Users.This is a global setting. it’s need pywin32 or cmd or regedit? please help me。 thanks






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top