简体   繁体   English

在ubuntu中双击文件时如何在文件上执行python脚本

[英]How to execute a python script over a file when double clicking on it in ubuntu

I have a python script that can open a particular kind of a file. 我有一个可以打开特定类型文件的python脚本。 It is an SPE file, opened using with the spe2py library ( https://pypi.org/project/spe2py/ ). 这是一个SPE文件,使用spe2py库( https://pypi.org/project/spe2py/ )打开。 I'd like, when I double click on a file of that kind, to launch the script passing to it the path of the file where I clicked. 我想,当我双击该类型的文件,以启动传递给它,我点击文件的路径脚本。 In this way my script could show the content of that file. 这样一来我的脚本可以显示文件的内容。 How could I do to do that? 我该怎么做? I am using Linux, Ubuntu 18.04. 我正在使用Linux,Ubuntu 18.04。

Thanks, 谢谢,

I use Linux Mint which is based on Ubuntu but it uses MATE (desktop environment) with file manager Caja (previously Nemo/Nautilus ) so last point (5) can be different on Ubuntu or other Linux . 我使用基于Ubuntu Linux Mint ,但它使用带有文件管理器Caja (以前为Nemo/Nautilus )的MATE (桌面环境),因此最后一点(5)在Ubuntu或其他Linux上可能有所不同。


You need few things: 您只需要几件事:

  1. script has to use sys.argv to get selected filename 脚本必须使用sys.argv来获取选定的文件名

  2. you have to add shebang in first line of script so system will know to use Python to run this script 您必须在脚本的第一行中添加shebang ,以便系统知道使用Python运行此脚本

#!/usr/bin/env python
  1. you have to set it executable so system will use shebang to run it (and you don't have to use python script.py ) 您必须将其设置为可执行文件,以便系统将使用shebang来运行它(并且您不必使用python script.py
chmod +x script.py
  1. you could put script in folder which is in PATH so system can run it in any folder without using full path to script. 您可以将脚本放在PATH中的文件夹中,以便系统可以在任何文件夹中运行它而无需使用脚本的完整路径。 On PATH could be folder ~/bin for your scripts - so you can put script in this folder. PATH可能是脚本的~/bin文件夹-因此您可以将脚本放在此文件夹中。

  2. in file manager Nemo/Nautilus/Caja you can right click on SPE file to see menu - there should be "Open with ... > Other program" and you can set your script. 在文件管理器Nemo/Nautilus/Caja您可以右键单击SPE文件以查看菜单-应该有"Open with ... > Other program"然后可以设置脚本。 You can use full path if you didn't put script in folder from PATH . 如果未将脚本从PATH放入文件夹,则可以使用完整路径。 If you use different file manager then you will have to search similar option 如果使用其他文件管理器,则必须搜索类似的选项

Nemo/Nautilus/Caja also sets variables with names of selected/highlighted files so you can run script with all files at once. Nemo/Nautilus/Caja还将设置具有选定/突出显示文件名称的变量,因此您可以一次对所有文件运行脚本。

CAJA_SCRIPT_NEXT_PANE_SELECTED_URIS
CAJA_SCRIPT_NEXT_PANE_SELECTED_FILE_PATHS
CAJA_SCRIPT_NEXT_PANE_CURRENT_URI
CAJA_SCRIPT_SELECTED_URIS
CAJA_SCRIPT_SELECTED_FILE_PATHS
CAJA_SCRIPT_CURRENT_URI

In script they should be avaliable 在脚本中它们应该可用

import os

print( os.environ['CAJA_SCRIPT_NEXT_PANE_SELECTED_URIS'] )

or safer if variable doesn't exists 如果不存在变量则更安全

import os

print( os.environ.get('CAJA_SCRIPT_NEXT_PANE_SELECTED_URIS') )

I would suggest you to run your python script using bash script program. 我建议您使用bash脚本程序运行python脚本。

You can make your bash script like this: 您可以像这样制作bash脚本:

Blockquote 块引用

#!/bin/sh
python3 python_script.py

Save this text file with .sh extension 使用.sh扩展名保存此文本文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM