Drag and drop batch file equivalent in Linux [clos

2019-07-29 12:31发布

问题:

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.