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

标签 python cocoa subprocess

我编写了一段 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/

相关文章:

cocoa - 创建类似 NSPopOver 的动画

cocoa - NSPersistentDocument 应用程序委托(delegate)

objective-c - AVCaptureVideoPreviewLayer方向错误

python - python中子进程命令行中没有反斜杠的引号

python - Pandas:帮助转换数据和编写更好的代码

python - 让 PyQt5 + venv + qt5ct 正常运行时遇到问题

Python - 子进程 - 如何在 Windows 中调用管道命令?

python subprocess32 超时,溢出错误

python - 类型错误 : 'DType' object is not callable

python - 在 NumPy 中创建随机 float 和 float64 数组