python - 如何在 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')
这完美运行并打开文件。
请帮我解决这个问题,我想我使用了错误的文件扩展名。文件类型为“文件”。
解决方案
从文档中os.startfile
:Start a file with its associated application.
Windows 必须在扩展.file
和某些软件之间建立关联。如果没有,Windows 不知道要使用什么软件。
您的.txt
文件在记事本(或其他)中打开,与.txt
扩展文件相关联。
添加关联:
- 您可以在系统的某处创建一个
.file
文件。 - 然后右键单击它,然后选择
Open with
。 - 选择应该处理
.file
扩展的软件,选中“始终将此应用程序用于这种类型”(或类似的东西,我没有要检查的 Windows)。
然后下次您尝试使用os.startfile()
时,它将file.file
使用所选应用程序打开。
推荐阅读
- git - 如何将提交推送到别人的私人仓库?(我有协作访问)
- javascript - 如何使半圆变大?
- java - 从 Jmenu/JPopupmenu 中的子菜单访问父文本
- html - 如何使用 v-model 和输入字段隐藏默认值?
- python - 使用 Keras-vis 和 tf-keras-vis 的激活最大化不适用于 MobileNetV2 模型
- c - C 可靠地获得 ULLONG_MAX 的百分比
- uicollectionview - 我们可以只将collectionview单元格内的图像拖放到UIView而不是移动整个collectionview单元格吗?
- ansible - Ansible 期望得到奇怪的符号
- mysql - 问题:无法关闭 macOS 上的 MAMP 服务器
- go - 注意 for Go 循环中的并发