grasingerm
grasingerm

Reputation: 1953

Executing a Fortran subprocess in Python with arguments

I've had success using the subprocess module in the past, however, I've struggled passing a Fortran executable an argument.

It works on the command-line: .\IMT.EXE path\to\file just fine. But when I try to use the subprocess module to pass it the exact same argument,

subprocess.call(['.\IMT.EXE', file_path])

The IMT tells me that the file inputted was invalid. Unfortunately the IMT does not output the invalid file name which makes debugging difficult. I know the file is there, I know IMT is being passed an argument, but for some reason the string isn't being passed correctly. I am currently downloading gcc on cygwin so that I can at least try to print the invalid file name (though I've never written fortran before).

Other things I've tried without success: this one just hangs and does nothing.

p = subprocess.Popen(['.\IMT.EXE'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
result = p.communicate(file_path.encode())

or this one just prints out Output: and then nothing.

p.stdin.write(temp_i_name.encode())
p.kill()
print('Output: ', p.stdout.read().decode())

Though I didn't believe it would help (and it didn't), I've also tried setting shell=True.

I've also tried os.system

os.system('.\IMT.EXE' + ' ' + file_path)

Same error:

Input Error.
Command line argument for Inverse Modeling Toolkit must
be valid path and filename of instruction file.

Other relevant information:

I've looked at other posts on SO about the subprocess module but none of them have helped.

EDIT:

so I've finally gotten it working. The problem was not improperly handling backslashes but that I had assumed when the subprocess IMT.EXE was opened that it would consider its directory as the current working directory. It does not. I should've been more clear as I was calling IMT: subprocess.call([os.path.join('imt', 'IMT.EXE'), file_path]) and only passing file_path as the file's name because it existed in directory imt. Sorry for the confusion.

Upvotes: 2

Views: 1736

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385910

My guess is, you're incorrectly passing the filename. For example, are you properly escaping backslashes? You say that you write the file before calling this program, are you remembering to flush the file so that it actually, physically on disk?

Can you show us exactly how you are defining the filename?

Another option: add a pdb breakpoint immediately before calling your external process. When your script stops at that point, open another window and see if the file is there.

Upvotes: 1

Related Questions