【问题标题】:How do I open a file in Python 3? [duplicate]如何在 Python 3 中打开文件? [复制]
【发布时间】:2021-10-21 07:47:11
【问题描述】:
如何在 Python 3 中打开文件,就像在不同的程序中打开文件一样,而不是作为文本文件。
我在网上找到的所有解决方案都是这样的
f1 = open("something.txt", "r")
print(f1.read())
f1.close()
但这只是输出文件内容。我想将 .exe 文件作为可执行文件打开。
我该怎么做?
【问题讨论】:
标签:
python
python-3.x
file
【解决方案1】:
如果你想执行一个 .exe 文件,你可以这样做:
import os
os.startfile("C:\Documents and Settings\flow_model\flow.exe")
您也可以查看subprocess
【解决方案2】:
变体1:启动程序
import os
os.system("something.exe")
变体 2:启动程序并读取其输出
import os
f1 = os.popen("something.exe")
print(f1.read())
变体3:在自定义程序(notepad.exe)中打开文本文件
import os
s = os.system('notepad.exe something.txt')
变体4:通过系统打开文本文件,在文件资源管理器中双击
import os
s = os.startfile('something.txt')