在 Python 中运行带有文本文件输出的 IDL 代码

Runnning an IDL code with text file output in Python

我有许多不同的代码,它们都将带有数据的文本文件作为输入,并写入不同的文件作为输出。其中三个代码是用 Python 2.7 编写的,但一个是用 IDL 编写的。我的目标是创建一个 "master" python 程序,通过键入 "python master.py" 可以 运行 所有这些代码。但是,由于我的系统限制,我无法使用 this question 中提到的 'pyIDL' 或 'pyIDLy' 模块。不确定这是否重要,但这是使用 linux 命令提示符。

目前我的 'master.py' 代码如下所示:

import os
os.system("python pycode_1.py")

os.system("idl")
os.system(".com idlcode.pro")
os.system(".r idlcode,"imputfile.dat"")
os.system("exit")

os.system("python pycode_2.py")
os.system("python pycode_3.py")

此代码 运行 是第一个 python 代码,可以正常进入 IDL。但是,它不会将后面的命令输入到 IDL 中。这意味着会出现 IDL 命令提示符,但我无法 运行 随后的 IDL 代码。

对于解决此问题的任何建议,我将不胜感激。 提前致谢!

所以我想出了一个似乎可以很好地解决这个问题的解决方案。上面的问题是使用 os.system 函数来做它不能做的事情。我的新代码是:

import os
import subprocess

os.system("python python_code1.py")

p=subprocess.Popen("idl", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(".com idlcode.pro\n")
p.stdin.write("idlcode\n")
p.stdin.write("exit")
p.wait()
os.system("python python_code2.py")
os.system("python python_code3.py")

如果您有 IDL 8.5 或更高版本,它会附带内置的 IDL-Python 桥。那么您的代码将类似于:

from idlpy import *
IDL.idlcode()

希望对您有所帮助。