1

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.

3
  • Isn't is possible to pass both the strings as command line arguments separated by space? Commented Jun 15, 2020 at 16:59
  • Just tried that, idrag.exe is opened in cmd but the files names are not input. Commented Jun 15, 2020 at 17:01
  • Your final Update/Solution was what I needed, thanks. I however did not need my text to be double-quoted like you did. It took me a long to realise this from the errors I got. I thus used "test1.in" instead of "'test1.in'". Commented Nov 25, 2023 at 10:27

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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_}')

1 Comment

This opens the program in cmd line, however the file names are not executed.
0

Try:

idrag.exe input.in output.out

Type it into the command line all at once.

12 Comments

Tried through a batch file and through the cmd line, it brought up idrag.exe, but nothing was executed.
Is idrag.exe a program you have written? I have never heard of it.
Have you checked the folder after running the program to see if anything has been generated?
No, it is an old fortran code that now can be run as .exe. It was written by a professor at Virginia Tech for analyzing drag on a wing.
I have checked the folder, no change. Typically the program just writes to the folder (so long as a flag has been checked to write to a file).
|
0

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"

3 Comments

I greatly appreciate this, I am however, slightly confused... I changed foo.txt and bar.txt to their respective names on my machine, but where do I go from there? Where should I run idrag.exe?
Just replace the call: process Popen(["idrag.exe"], stdin=PIPE, stdout=PIPE, text=True). I did not want to download your .exe
Ohhhhhhhhhhhhh I think the world makes sense now. I understand what you're saying now. The process did not really work, but there were no errors so thats a win? Unsure, I did find a solution though. Thanks!!
0

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

2 Comments

This is kind of working! Two problems though: First, I cannot input the output.out file because cmd says the file cannot be found (it hasn't been written yet). Second: I receive an error I've never seen before. forrtl: severe (43): file name specification error, unit 10, file C:\WhateverMyDirectoryIs. A bunch of stuff incrementally linked image--PC correlation disabled.
Well done, seems you managed it. I am interested if my solution would have worked with single quotes around 'input.in' and 'output.out'...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.