In python, how do I drag and drop 1 or more files onto my script as arguments with absolute path? (for windows, linux, and mac)

故事扮演 提交于 2019-12-21 17:56:14

问题


I am writing a simple Python script with no GUI. I want to be able to drag and drop multiple files onto my python script and have access to their absolute paths inside of the script. How do I do this in Mac, Linux, and windows? For times sake, just Mac will be fine for now.

I've googled this question and only found one related one but it was too confusing. I am currently running Mac OS X Snow Leopard.

Any help is much appreciated.

Thanks!


回答1:


For OS X, the most straightforward way is to have your script run as part of an application bundle (.app). You can use something like py2app to build a python application. Another approach might be to use Automator or AppleScript to create an app that takes the input parameters and passes them to the python script. An example of Automator usage is here.




回答2:


Usually when you drag a file onto a script/executable, the OS passes the path to that file as a command-line argument. Check sys.argv




回答3:


This really is independent of python. It depends entirely on which file browser you're using and how it supports drag and drop.



来源:https://stackoverflow.com/questions/2660291/in-python-how-do-i-drag-and-drop-1-or-more-files-onto-my-script-as-arguments-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!