如何 运行 基于命令行的应用程序和输入文本?
How to run a command line based application and input text?
我有一个名为 idrag.exe 的程序,它完全基于命令行。它至少需要 2 个输入,每个输入都用引号引起来,'input.in' 和 'output.out' 都由它们自己执行。 input.in 是以前创建的文件,output.out 是程序写入的文件。我怎样才能打开这个程序,输入“'input.in'”回车,输入“'output.out'”然后回车?我可以通过os.system打开程序,但是我没能使子进程正常工作。
我遇到的一个大问题是 idrag.exe 打开后无法输入。我知道我可以制作一个包含命令列表的 .bat 文件,但我不是在输入命令,而是在输入文本。
编辑:
对于那些感兴趣的人 here is the .exe direct download link. Here is a link to a sample input file
编辑 2:
通常我 运行 idrag.exe,输入“'input.in'”回车,输入“'output.out'”回车,idrag 计算其内容并写入 output.out到当前目录。
编辑 3(这可能有点过分了):
Here is a website compiling a bunch of codes, you can ctrl + f to find idrags section. It includes all 3 fortran codes, the executable, and a bunch of sample in/out files. 我决不期望任何人真正经历一切并尝试它,但它在这里是为了防止你感到无聊。
最终更新/解决方案
哇哦,我们都做到了。原来有一个 api 让事情变得简单!它被称为预期。
from pexpect import popen_spawn
process = popen_spawn.PopenSpawn('idrag.exe')
process.sendline("'test1.in'")
process.sendline("'test1.out'")
注意:popen_spawn 不会自动调用 pexpect,所以你必须专门调用它。此外,由于 unix 的原因,windows 上的语法与 Mac 上的语法略有不同。
尝试:
idrag.exe input.in output.out
一次性全部输入命令行。
正如其他人所说,如果 'input.in' 和 'output.out' 不改变,您可以这样做:
os.system('idrag.exe input.in output.out')
也许您的意思是您希望能够使用 python 指定 input.in 和 output.out 的值?
in_ = input("Enter input file name")
out_ = input("Enter output file name")
os.system(f'idrag.exe {in_} {out_}')
尝试
os.system("idrag.exe < 'input.in' < 'output.out'")
我认为问题在于,一旦程序 运行,您希望自动输入,这与输入命令行参数感觉相同,但在技术上有所不同。
见此 question
由于我不想下载 EXE 文件,我将尝试使用 Windows 要求输入两个字符串的批处理文件来模拟上述 EXE。
给定 "idrag.bat" 作为
@echo off
set /p foo=enter input file:
set /p bar=enter output file:
echo %date%>%bar%
echo %time%>>%bar%
echo %foo%>>%bar%
type %bar%
我们可以使用下面的python脚本将"foo.txt"和"bar.txt"传递给"idrag.bat"
from subprocess import Popen, PIPE
process = Popen(["idrag.bat"], stdin=PIPE, stdout=PIPE, text=True)
print(process.communicate(input='foo.txt\nbar.txt\n')[0])
要进行测试,请将对 "idrag.bat" 的调用替换为 "idrag.exe"
有一个 api 让事情变得简单!它被称为预期。
from pexpect import popen_spawn
process = popen_spawn.PopenSpawn('idrag.exe')
process.sendline("'test1.in'")
process.sendline("'test1.out'")
注意:popen_spawn 不会自动从 pexpect 调用,因此您必须专门调用它。此外,由于 unix 的原因,windows 上的语法与 Mac 上的语法略有不同。
我有一个名为 idrag.exe 的程序,它完全基于命令行。它至少需要 2 个输入,每个输入都用引号引起来,'input.in' 和 'output.out' 都由它们自己执行。 input.in 是以前创建的文件,output.out 是程序写入的文件。我怎样才能打开这个程序,输入“'input.in'”回车,输入“'output.out'”然后回车?我可以通过os.system打开程序,但是我没能使子进程正常工作。
我遇到的一个大问题是 idrag.exe 打开后无法输入。我知道我可以制作一个包含命令列表的 .bat 文件,但我不是在输入命令,而是在输入文本。
编辑:
对于那些感兴趣的人 here is the .exe direct download link. Here is a link to a sample input file
编辑 2:
通常我 运行 idrag.exe,输入“'input.in'”回车,输入“'output.out'”回车,idrag 计算其内容并写入 output.out到当前目录。
编辑 3(这可能有点过分了):
Here is a website compiling a bunch of codes, you can ctrl + f to find idrags section. It includes all 3 fortran codes, the executable, and a bunch of sample in/out files. 我决不期望任何人真正经历一切并尝试它,但它在这里是为了防止你感到无聊。
最终更新/解决方案
哇哦,我们都做到了。原来有一个 api 让事情变得简单!它被称为预期。
from pexpect import popen_spawn
process = popen_spawn.PopenSpawn('idrag.exe')
process.sendline("'test1.in'")
process.sendline("'test1.out'")
注意:popen_spawn 不会自动调用 pexpect,所以你必须专门调用它。此外,由于 unix 的原因,windows 上的语法与 Mac 上的语法略有不同。
尝试:
idrag.exe input.in output.out
一次性全部输入命令行。
正如其他人所说,如果 'input.in' 和 'output.out' 不改变,您可以这样做:
os.system('idrag.exe input.in output.out')
也许您的意思是您希望能够使用 python 指定 input.in 和 output.out 的值?
in_ = input("Enter input file name")
out_ = input("Enter output file name")
os.system(f'idrag.exe {in_} {out_}')
尝试
os.system("idrag.exe < 'input.in' < 'output.out'")
我认为问题在于,一旦程序 运行,您希望自动输入,这与输入命令行参数感觉相同,但在技术上有所不同。
见此 question
由于我不想下载 EXE 文件,我将尝试使用 Windows 要求输入两个字符串的批处理文件来模拟上述 EXE。
给定 "idrag.bat" 作为
@echo off
set /p foo=enter input file:
set /p bar=enter output file:
echo %date%>%bar%
echo %time%>>%bar%
echo %foo%>>%bar%
type %bar%
我们可以使用下面的python脚本将"foo.txt"和"bar.txt"传递给"idrag.bat"
from subprocess import Popen, PIPE
process = Popen(["idrag.bat"], stdin=PIPE, stdout=PIPE, text=True)
print(process.communicate(input='foo.txt\nbar.txt\n')[0])
要进行测试,请将对 "idrag.bat" 的调用替换为 "idrag.exe"
有一个 api 让事情变得简单!它被称为预期。
from pexpect import popen_spawn
process = popen_spawn.PopenSpawn('idrag.exe')
process.sendline("'test1.in'")
process.sendline("'test1.out'")
注意:popen_spawn 不会自动从 pexpect 调用,因此您必须专门调用它。此外,由于 unix 的原因,windows 上的语法与 Mac 上的语法略有不同。