简体   繁体   English

如何在Python中从文本文件打开程序

[英]How to open a program from a text file in Python

How would I go about opening a file in Python by referring to a text file. 如何通过引用文本文件在Python中打开文件。 EXAMPLE: 例:

    f.open('openthis.txt')

Then I would have openthis.txt in the same folder that would say: 然后,我将在同一文件夹中显示openthis.txt:

C:\\Folder\\myprogram.exe C:\\ Folder \\ myprogram.exe

Therefore the code opens up myprogram.exe from the directory 因此,该代码从目录打开了myprogram.exe。

I want to do this so the code is easily changable, instead of having to edit it in IDLE every time I want to change the file I open. 我想这样做是为了使代码易于更改,而不是每次我要更改打开的文件时都必须在IDLE中对其进行编辑。

You want the subprocess module. 您需要子流程模块。 Specifically, you'd do something like: 具体来说,您可以执行以下操作:

import subprocess

with open("inputfile", "rb") as f:
    subprocess.call(f.read())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM