首页 > 解决方案 > 如何在 python 中打开“文件”类型的文件?

问题描述

import os
os.startfile('C:\\Some Location\\file.file')

当我尝试运行上述代码时,代码给出如下错误:- FileNotFoundError: [WinError 2] The system cannot find the file specified

虽然对于同一位置,此代码非常适用于具有“.txt”扩展名的文件。例如,

import os
os.startfile('C:\\Some Location\\file.txt')

这完美运行并打开文件。

请帮我解决这个问题,我想我使用了错误的文件扩展名。文件类型为“文件”。

标签: pythonpython-3.xpython-2.7file

解决方案


从文档中os.startfileStart a file with its associated application.

Windows 必须在扩展.file和某些软件之间建立关联。如果没有,Windows 不知道要使用什么软件。

您的.txt文件在记事本(或其他)中打开,与.txt扩展文件相关联。

添加关联:

  • 您可以在系统的某处创建一个.file文件。
  • 然后右键单击它,然后选择Open with
  • 选择应该处理.file扩展的软件,选中“始终将此应用程序用于这种类型”(或类似的东西,我没有要检查的 Windows)。

然后下次您尝试使用os.startfile()时,它将file.file使用所选应用程序打开。


推荐阅读