使用 Python 脚本打开特定文件类型?

发布于 2024-12-23 18:58:35 字数 155 浏览 1 评论 0原文

如何使 Python 脚本成为特定文件类型(例如 *.foo)的默认应用程序?例如,当我双击 Finder/Explorer 中的文件时,我希望该文件在 Python 脚本中打开。

这可以在 Win 和/或 OS X 中实现吗?如果重要的话,该应用程序是 PySide 应用程序。

How can I make a Python script to be a specific file type's (e.g., *.foo) default application? As in, when I double click the file in the Finder / Explorer I want the file to open in the Python script.

Is this possible to do in Win and/or OS X? The application is a PySide app if that matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苦笑流年记忆 2024-12-30 18:58:35

Mac OS X

Mac OS X 上,您可以使用 Automator 创建一个应用程序,该应用程序调用您的 python 应用程序并将输入文件路径作为字符串参数传递。在应用程序工作流向导中,添加操作“运行 Shell 脚本”,选择传递输入:作为作为参数,然后在文本框中添加:

python /path/to/my/app/myapp.py "$@"

"$@ " 将输入(也称为所选文件)中的任何参数作为字符串传递。只要您的脚本设置为将输入 (sys.argv) 作为字符串列表(第一个是 python 应用程序路径)进行处理,那么它就可以工作。

当您保存 Automator 工作流程时,OS X 会将其视为任何其他应用程序,并且您可以将该应用程序设置为“*.foo”类型文件的默认应用程序。要将“*.foo”与该应用程序关联,请右键单击 .foo 文件,获取信息打开方式:其他...,选择您在 Automator 中创建的应用程序,然后单击更改全部... 按钮。

Windows

一种类似但希望涉及较少的方法可能适用于 Windows。您可以使用以下内容创建一个批处理文件 (.bat):

python C:\path\to\my\app\myapp.py %*

%* 扩展到所有参数。

只要您可以将文件扩展名与该批处理文件相关联,那么您就可以做到这一点,这就是您的解决方案。不过,我还没有尝试过这个 Windows 解决方案,所以对此持保留态度。另一方面,我已经测试过 Mac 解决方案。

Mac OS X

On Mac OS X you can use Automator to create an application that calls your python app and passes the input file path as a string argument. In the application workflow wizard, add action "Run Shell Script", select Pass input: as as arguments, and in the text box add:

python /path/to/my/app/myapp.py "$@"

The "$@" passes along whatever arguments were in the input (aka the selected file) as strings. As long as your script is set up to deal with the input (sys.argv) as a list of strings (the first one being the python app path), then it will work.

When you save that Automator workflow, it is treated by OS X like any other app, and you can set that app as the default for files of type "*.foo". To associate "*.foo" with that app, right click a .foo file, Get Info, Open with: Other..., choose the app you created in Automator, then click the Change All... button.

Windows

A similar but hopefully less-involved approach might work in Windows. You could probably create a batch file (.bat) with the following:

python C:\path\to\my\app\myapp.py %*

The %* expands to all arguments.

As long as you can associate a file extension with that batch file, then you could do that, and that's your solution. However, I haven't tried this Windows solution, so take it with a grain of salt. The Mac solution, on the other hand, I have tested.

水中月 2024-12-30 18:58:35

例如,这是我编写的通用解决方案:
1) 打开已复制到 Linux 盒子的 Windows 桌面链接 (*.URL)。
或者
2) 打开已复制到 Windows 盒子的 Linux .Desktop 链接。

下面是处理这两种情况的 Python 脚本:

# UseDesktopLink.py
import sys
import webbrowser
script, filename = sys.argv
file_object = open(filename,'r')
for line in file_object:
    if line[0:4]=="URL=":
        url=line[4:]
        webbrowser.open_new(url)
file_object.close()

在 Windows 上,使用 Scott H 的方法(通过 bat 文件)来处理关联。

在 Linux 上,右键单击 Windows URL 文件。选择属性,然后打开方式。单击“添加”以添加新应用程序。然后在“添加应用程序”窗口的底部,单击“使用自定义命令”。然后浏览到 UseDesktopLink.py 文件并单击“打开”。但在单击“添加”之前,在“使用自定义命令”下方的文本框中,在文件名前添加“python ”(不带引号)。然后单击添加并关闭。

希望有帮助。

By example, here's a universal solution I wrote for:
1) opening a Windows desktop link (*.URL) that's been copied to a Linux box.
Or
2) opening a Linux .Desktop link that's been copied to a Windows box.

Here's the Python script that handles both cases:

# UseDesktopLink.py
import sys
import webbrowser
script, filename = sys.argv
file_object = open(filename,'r')
for line in file_object:
    if line[0:4]=="URL=":
        url=line[4:]
        webbrowser.open_new(url)
file_object.close()

On Windows, use Scott H's method (via a bat file) to handle the association.

On Linux, right-click a Windows URL file. Choose Properties, and Open With. Click Add to add a new application. Then at the bottom of the "Add Application" window, click "Use a custom command". Then browse to the UseDesktopLink.py file and click Open. But before you click Add, in the textbox below "Use a custom command", put "python " before the filename (without the quotes). Then click Add and Close.

Hope that helps.

王权女流氓 2024-12-30 18:58:35
  1. 找到 任何 类型为 foo 的文件,
  2. 右键单击 ->; 获取信息或单击文件图标,然后单击获取信息或单击文件并
  3. 打开中 点击Command+I在显示的 窗格中,选择 python 二进制文件的路径
  4. 选择后,您可以单击全部更改按钮,
  5. 它会要求确认,只需说继续
  1. Find any file of type foo
  2. right-click -> Get Info or Click on the file icon,then click Get info or click on the file and hit Command+I
  3. In the Open With pane that shows up, select the path to the python binary
  4. Once selected, You can click the change All button
  5. It'll ask for confirmation, just say continue
谈情不如逗狗 2024-12-30 18:58:35

我在自己寻找答案时发现了这个老问题,我想我会分享我的解决方案。我使用一个简单的 c 程序将参数定向到 python 脚本,允许 python 脚本保留为脚本,而不需要编译它来使事情正常工作。这是我的 C 程序:

int main(int argc, char *argv[]){
    char cmd[0xFF];
    // For me, argv[1] is the location of the file that is being opened.  I'm not sure if this is different on other OSes
    snprintf(cmd,sizeof cmd,"python YOUR_PYTHON_SCRIPT_HERE.py -a %s", argv[1]);
    system(cmd);
    return 0;
}

然后我编译了该 C 程序并将其设置为文件扩展名的默认应用程序。
然后,在 python 脚本 YOUR_PYTHON_SCRIPT_HERE.py 中,我收到如下参数:

import sys
assert len(sys.argv) > 2 # Breaks if you call the script without the arguments
theFile = " ".join(sys.argv[2:]) # What the c program gives us 
print(theFile) # Print it out to prove that it works

theFile 将包含正在打开的文件的位置
使用以下命令获取文件的内容:

with open(theFile,"r") as f:
    fileContents = f.read()

I found this old question while looking for an answer myself, and I thought I would share my solution. I used a simple c program to direct the arguments to a python script, allowing the python script to stay a script instead of needing to compile it to make things work. Here is my c program:

int main(int argc, char *argv[]){
    char cmd[0xFF];
    // For me, argv[1] is the location of the file that is being opened.  I'm not sure if this is different on other OSes
    snprintf(cmd,sizeof cmd,"python YOUR_PYTHON_SCRIPT_HERE.py -a %s", argv[1]);
    system(cmd);
    return 0;
}

I then compiled the c program and set that as the default application for the file extension.
Then, in the python script YOUR_PYTHON_SCRIPT_HERE.py, I receive the argument like this:

import sys
assert len(sys.argv) > 2 # Breaks if you call the script without the arguments
theFile = " ".join(sys.argv[2:]) # What the c program gives us 
print(theFile) # Print it out to prove that it works

theFile will contain the location of the file that is being opened
Get the contents of the file by using:

with open(theFile,"r") as f:
    fileContents = f.read()
掩耳倾听 2024-12-30 18:58:35

在 Windows 上:

  1. 右键单击该文件(本例中我使用了 .docx 文件)
  2. 选择 Open with...
  3. 从应用程序列表中选择 python code>
  4. 可选:选择始终使用选定的程序打开此类文件

注意:这将在 python shell 上下文中运行.docx文件的内容。一旦完成评估文件内容,它将立即关闭。如果您想在文字处理器中编辑文件,也许您应该下载 notepad++,然后选择该应用程序作为默认应用程序。

On Windows:

  1. Right click the file (I used a .docx file for this example)
  2. Select Open with...
  3. From the applications list, select python
  4. Optional: Select the Always use the selected program to open this kind of file.

Note: this will run the contents of the .docx file in context of the python shell. It will immediately close once it is finished evaluating the contents of the file. If you'd like to edit the file in a word processor, perhaps you should download notepad++, and select that application as the default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文