2

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:

  • Windows 7
  • Python 3.3
  • IMT was written in Fortran90
  • I have the IMT source code, but don't know anything about Fortran.

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.

6
  • 3
    Don't use backslashes. If you do, use raw strings. Commented Dec 12, 2012 at 15:00
  • could you perhaps print what file_path is? Protip: Remember to always escape backslashes Commented Dec 12, 2012 at 15:01
  • Also try opening the file_path from inside python, to verify that it really is there. You use relative paths, you might not be where you think you are. Commented Dec 12, 2012 at 15:03
  • Sorry I know path\to\file isn't very specific. I'm writing the file before I execute so I know it is there. I do print what file_path is and have confirmed it to be correct. There are no backslashes because the file is being saved in the same directory as IMT.EXE before the subprocess is called. I have confirmed it is there and actually ran it from the command-line after the python script closed and it ran successfully. Thanks for the ideas though. Commented Dec 12, 2012 at 15:11
  • 1
    I always use os.path.join when generating file paths because slashes are always a pain. Commented Dec 12, 2012 at 15:12

1 Answer 1

1

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.

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

Comments

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.