gpt4 book ai didi

python - 运行 subprocess.call 来运行 Cocoa 命令行应用程序

转载 作者:行者123 更新时间:2023-12-03 18:04:40 25 4
gpt4 key购买 nike

我编写了一段 Cocoa 代码,它接收一个包含边界框的 XML 文件,然后将其绘制在视频顶部(每个框都有一个关联的帧)。 Cocoa 程序旨在从命令行运行(并将其所有参数作为命令行参数)

我可以使用任何 XML 文档运行程序。但是,当我尝试从 Python 脚本中运行该程序时,遇到了问题。例如:

with file("test.xml") as temp:
temp.write(doc.toprettyxml())
# cval is my cocoa program to call, the other arguments are given to the Python script and parsed with optparser
command = ["./cval", "-o", options.output, "-i", str(options.interval), "-s", "%dx%d" % (options.width, options.height), "-f", str(options.frames), "-x", temp.name]
subprocess.call(command)

有时这会导致我的“cval”失败,有时则不会(更改 XML 文档中的一个数字可以改变其行为)。当尝试读取不存在的 XML 元素时,我还可以验证它是否损坏。只是,我可以打开“test.xml”,并验证该元素确实存在。

但是,如果我自己使用“test.xml”运行“cval”(在 Python 脚本之外),则它可以正常工作。这让我相信当我执行“subprocess.call”时会发生一些奇怪的事情,但我不确定它可能是什么。我还有其他 Cocoa/Python 混合,它们执行完全不同的任务(即不使用 XML),它们也任意表现出奇怪的行为,但本质上更复杂。

我希望有人也遇到过这个问题,或者可能知道调试这个奇怪现象的下一步。

最佳答案

由于代码最初使用临时文件,因此在将文件传递给子进程之前我无法关闭该文件。但是,我应该做的是在调用 subprocess.call 之前刷新文件。不一致的行为可能是由于输入的大小导致不同阈值下的自动刷新造成的。

代码应为:

with file("test.xml") as temp:
temp.write(doc.toprettyxml())
temp.flush()
command = ["./cval", "-o", options.output, "-i", str(options.interval), "-s", "%dx%d" % (options.width, options.height), "-f", str(options.frames), "-x", temp.name]
subprocess.call(command)

关于python - 运行 subprocess.call 来运行 Cocoa 命令行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3283773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com