首页 > 解决方案 > 如何将 .txt 文件 arg 传递给 exe 应用程序?

问题描述

我需要将 .txt 文件与 .exe 应用程序结合起来。当我在 .exe 应用程序上移动 .txt 时,此过程在我的桌面上正常工作。我可以用python做到这一点吗?

标签: pythonpython-3.x

解决方案


是的,你只是这样做

from subprocess import call
call(["that.exe", "that.txt"])

或者

import os
os.system("that.exe that.txt")

编辑:

也许你需要打电话cmd \c来运行exe?

from subprocess import call
call(["cmd", "/c", "that.exe", "that.txt"])

或者

import os
os.system("cmd /c that.exe that.txt")

编辑:

如果您想在调用进程后将击键发送到进程,您可以使用 subprocess 来获取可以跟进的对象。

import subprocess
proc = subprocess.Popen(["cmd", "/c", "that.exe", "that.txt"], shell=True)
proc.communicate(input=b'\n')

最终编辑:

你可能需要使用 stdin=subprocess.PIPE ......

import subprocess
proc = subprocess.Popen(["cmd", "/c", "that.exe", "that.txt"] ,stdin=subprocess.PIPE)
proc.communicate(input=b'\n')

推荐阅读