简体   繁体   中英

How to run a command line based application and input text?

I have a program called idrag.exe that is entirely command line based. It takes a minimum of 2 inputs with quotes around each, 'input.in' and 'output.out' both of which are executed by themselves. input.in is a file previously created and output.out is a file the program writes. How can I open this program, type " 'input.in' " hit enter, type " 'output.out' " and hit enter? I can open the program through os.system, but I haven't managed to get the sub process to work properly.

A big problem I am having is the ability to type once idrag.exe is open. I know I can make a.bat file with a list of commands, but I'm not inputting commands, I'm inputting text.

Edit:

For those interested here is the.exe direct download link . Here is a link to a sample input file

Edit 2:

Usually I run idrag.exe, type " 'input.in' " hit enter, type " 'output.out' " hit enter, and idrag computes its stuff and writes output.out to the current directory.

Edit 3 (this might be getting excessive):

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. By no means do I expect anyone to actually go through everything and try it, but its here incase you're bored.

Final Update / Solution

Woohoo, we've done it everyone. Turns out there is an api to make things like this easy. It is called pexpect.

from pexpect import popen_spawn


process = popen_spawn.PopenSpawn('idrag.exe')
process.sendline("'test1.in'")
process.sendline("'test1.out'")

Note: popen_spawn does not automatically call from pexpect, so you have to specifically call for it. Also, syntax is slightly different on windows than it is on Mac due to unix stuff.

Try:

idrag.exe input.in output.out

Type it into the command line all at once.

As others have said, if 'input.in' and 'output.out' don't change, you can just do:

os.system('idrag.exe input.in output.out')

Maybe you mean you want to be able to specify the value of input.in and output.out using python?

in_ = input("Enter input file name")
out_ = input("Enter output file name")

os.system(f'idrag.exe {in_} {out_}')

Try

os.system("idrag.exe < 'input.in' < 'output.out'")

I think the issue is that you want to automate inputs once the program is running, which feels the same but is technically different from entering command line parameters.

See thius question

Since I do not want to download an EXE file, I'll try to simulate said EXE with a Windows batch file that asks for two strings to be typed in.

Given "idrag.bat" as

@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%

We can use the following python script to pass "foo.txt" and "bar.txt" to "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])

To test, replace the call to "idrag.bat" with "idrag.exe"

There is an api to make things like this easy. It is called pexpect.

from pexpect import popen_spawn


process = popen_spawn.PopenSpawn('idrag.exe')
process.sendline("'test1.in'")
process.sendline("'test1.out'")

Note: popen_spawn does not automatically call from pexpect, so you have to specifically call for it. Also, syntax is slightly different on windows than it is on Mac due to unix stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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