Ibe
Ibe

Reputation: 6035

How to run executable with input file using Python?

I am running an executable from cmd:

*.exe input.inp

I want to run it using python and tried following:

os.system('"*.exe"') 

But don't know how to specify input file as well. Any suggestions?

Upvotes: 0

Views: 2855

Answers (3)

user3376851
user3376851

Reputation: 159

import os
os.system(r'pathToExe.exe inputFileOrWhateverOtherCommand')

Upvotes: 0

Ibe
Ibe

Reputation: 6035

I had to launch cmd window and specify input file location from within Python script. This page was really helpful in getting it done.

I used Popen(['cmd', '/K', 'command']) from above page and replaced '/K' with '/C' in it to run and close the cmd window.

Upvotes: 0

Abhishek Dey
Abhishek Dey

Reputation: 1639

import os
from subprocess import Popen, PIPE

p = Popen('fortranExecutable', stdin=PIPE) #NOTE: no shell=True here
p.communicate(os.linesep.join(["input 1", "input 2"]))

For more please refer to:

Using Python to run executable and fill in user input

Upvotes: 1

Related Questions