Drop Files on a Python Script
So I have always been wondering how you can create almost like a ‘droplet’ to steal the photoshop lingo, from a python script. A while ago I came across some sites showing how to edit shellex in regedit to allow for files to be dropped on any python script and fed to it as args (Windows).
It’s really simple, you grab this reg file [py_drag_n_drop.reg] and install it.
Now when you drop files onto a python script, their filenames will be passed as args, here’s a simple script to test.
import sys f = open('c:\\tmp.txt', 'w') for arg in sys.argv: f.write(arg + '\n') f.close() |
When you save this, and drop files onto its icon, it will create tmp.txt, which will look like this:
X:\projects\2010\python\drag_and_drop\drag_n_drop.py X:\photos\2010.04 - easter weekend\fuji\DSCF9048.MPO X:\photos\2010.04 - easter weekend\fuji\DSCF9049.MPO X:\photos\2010.04 - easter weekend\fuji\DSCF9050.MPO X:\photos\2010.04 - easter weekend\fuji\DSCF9051.MPO X:\photos\2010.04 - easter weekend\fuji\DSCF9052.MPO |
The script itself is the first arg, then all the files. This way you can easily create scripts that accept drops to do things like convert files, upload files, etc..
Fun — I am having difficulty installing py_drag_n_drop.reg on a mac–
Comment by Thomas Murphy — 2017/04/03 @ 12:41 PM