使用 Python 脚本打开特定文件类型?
如何使 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Mac OS X
在 Mac OS X 上,您可以使用 Automator 创建一个应用程序,该应用程序调用您的 python 应用程序并将输入文件路径作为字符串参数传递。在应用程序工作流向导中,添加操作“运行 Shell 脚本”,选择
传递输入:
作为作为参数
,然后在文本框中添加:"$@ "
将输入(也称为所选文件)中的任何参数作为字符串传递。只要您的脚本设置为将输入 (sys.argv
) 作为字符串列表(第一个是 python 应用程序路径)进行处理,那么它就可以工作。当您保存 Automator 工作流程时,OS X 会将其视为任何其他应用程序,并且您可以将该应用程序设置为“*.foo”类型文件的默认应用程序。要将“*.foo”与该应用程序关联,请右键单击 .foo 文件,
获取信息
,打开方式:其他...
,选择您在 Automator 中创建的应用程序,然后单击更改全部...
按钮。Windows
一种类似但希望涉及较少的方法可能适用于 Windows。您可以使用以下内容创建一个批处理文件 (
.bat
):%*
扩展到所有参数。只要您可以将文件扩展名与该批处理文件相关联,那么您就可以做到这一点,这就是您的解决方案。不过,我还没有尝试过这个 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:
asas arguments
, and in the text box add: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 theChange 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: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.
例如,这是我编写的通用解决方案:
1) 打开已复制到 Linux 盒子的 Windows 桌面链接 (*.URL)。
或者
2) 打开已复制到 Windows 盒子的 Linux .Desktop 链接。
下面是处理这两种情况的 Python 脚本:
在 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:
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.
foo
的文件,获取信息
或单击文件图标,然后单击获取信息或单击文件并打开中 点击Command+I在显示的
窗格中,选择 python 二进制文件的路径全部更改
按钮,继续
foo
Get Info
or Click on the file icon,then click Get info or click on the file and hit Command+IOpen With
pane that shows up, select the path to the python binarychange All
buttoncontinue
我在自己寻找答案时发现了这个老问题,我想我会分享我的解决方案。我使用一个简单的 c 程序将参数定向到 python 脚本,允许 python 脚本保留为脚本,而不需要编译它来使事情正常工作。这是我的 C 程序:
然后我编译了该 C 程序并将其设置为文件扩展名的默认应用程序。
然后,在 python 脚本 YOUR_PYTHON_SCRIPT_HERE.py 中,我收到如下参数:
theFile 将包含正在打开的文件的位置
使用以下命令获取文件的内容:
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:
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:
theFile will contain the location of the file that is being opened
Get the contents of the file by using:
在 Windows 上:
.docx
文件)Open with...
python
code>始终使用选定的程序打开此类文件
。注意:这将在 python shell 上下文中运行
.docx
文件的内容。一旦完成评估文件内容,它将立即关闭。如果您想在文字处理器中编辑文件,也许您应该下载 notepad++,然后选择该应用程序作为默认应用程序。On Windows:
.docx
file for this example)Open with...
python
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.