Python 打印缓冲

1 投票
2 回答
781 浏览
提问于 2025-04-15 16:33

让我重新表述一下我之前的问题。

我刚刚在ArcGIS中创建了一个工具,使用Python作为脚本语言。这个工具通过subprocess.popen来执行一个外部程序。当我从ArcGIS运行这个工具时,会弹出一个窗口,只显示以下内容。

Executing: RunFLOW C:\FLOW C:\FLOW\FLW.bat
Start Time: Mon Nov 30 16:50:37 2009
Running script RunFLOW...
Completed script RuFLOW...
Executed (RunFLOW) successfully.
End Time: Mon Nov 30 16:50:48 2009 (Elapsed Time: 11.00 seconds)

脚本如下

# Import system modules
import sys, string, os, arcgisscripting, subprocess
# Create the Geoprocessor object
gp = arcgisscripting.create()
# Read the parameter values:
#  1: input workspace
prj_fld = gp.GetParameterAsText(0)
Flow_bat = gp.GetParameterAsText(1)
os.chdir(prj_fld)
p=subprocess.Popen(Flow_bat,shell=True,stdout=subprocess.PIPE)
stdout_value = p.communicate()[0]
print '\tstdout:', repr(stdout_value)

当我从命令窗口运行同样的程序时,它会打印出一屏幕的信息(日期、迭代次数等)。我希望在从ArcGIS运行模型后弹出的窗口中,除了现在打印的内容外,还能看到所有这些信息。

我尝试了print、communicate、flush等方法,但都没有成功。有什么建议吗?

当我现在以这种方式运行脚本时,它确实运行了可执行文件,但出现了以下错误

ERROR 999998: There are no more files.

谢谢

2 个回答

0

我不知道你是否知道这一点,但我使用:

gp.AddMessage('blah blah')

这个代码来让消息出现在ArcGIS的处理窗口里。这是一个叫做geoprocessor的对象的方法,这个对象是你在Python中导入的库,用来访问ArcGIS引擎。如果你在做任何地理处理的话,你应该已经导入了这个库。

0

我对ArcGIS一点都不了解,所以可能说的不是很准确,但如果你想要标准输出(stdout),通常不应该使用communicate()这个方法。你应该用类似下面的方式:

p=subprocess.Popen(Flow_bat,shell=True,stdout=subprocess.PIPE)
stdout_value = p.stdout.read()

communicate()这个方法是用来和一个进程进行交互的。从文档上来看:

Interact with process: Send data to stdin.  Read data from stdout
and stderr, until end-of-file is reached.  Wait for process to
terminate.

我猜当ArcGIS运行你的脚本时,stdin没有连接,这可能导致脚本因为某种原因退出。

撰写回答