Drag and drop batch file equivalent in Linux [closed]

时光毁灭记忆、已成空白 提交于 2020-01-05 05:45:08

问题


I am currently able to run files through my python script via a .bat file. The contents of this batch file is simply:

C:\Python26\python.exe mypythonfile.py %1
pause

I can drag and drop various files onto the .bat file and the python script will execute, using the dropped file as the argument...using sys.argv

This is fine for windows. But what I want to do now, is do the equivalent in Linux.

Any help would be greatly appreciated.


回答1:


the equivalent of your script on a bourne compatible shell (like bash, dash, zsh and most other shells you ever find on linux) would be:

#!/bin/sh
python /path/to/my/pythonfile.py $1
read

in practice this is not really needed, as for drag-n-drop you would create an application "shortcut" on your desktop that simply contains python /path/to/my/pythonfile.py $1 as the application string. and be done with it.

the really clean solution would even add the shebang #!/usr/bin/python to the top of your .py-file and make it executable - so you can run it directly as

/path/to/my/pythonfile.py $1

integrating the script with your GUI (icon, where you can drop things at), is depending on the actual desktop environment you use.

xfce4

create a new launcher (e.g. right-click on a panel, "add new element", choose Starter; then right click on the newly created Starter-Icon, click on "add new object", give it a name and in the cmdline-field add

/full/path/to/my/script.sh %U

the %U will be replaced with whatever you drop on the icon.




回答2:


You don't need a separate script if you start your python file with something like:

#!/usr/bin/python

Then you can simply drag & drop things on the python script itself.



来源:https://stackoverflow.com/questions/19978540/drag-and-drop-batch-file-equivalent-in-linux

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