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.
path\to\fileisn't very specific. I'm writing the file before I execute so I know it is there. I do print whatfile_pathis and have confirmed it to be correct. There are no backslashes because the file is being saved in the same directory asIMT.EXEbefore thesubprocessis 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.os.path.joinwhen generating file paths because slashes are always a pain.