从 python 调用非 python 程序?

发布于 2024-09-02 19:16:06 字数 485 浏览 3 评论 0原文

我目前正在努力从 python 脚本调用非 python 程序。

我有大约 1000 个文件,当它们通过这个 C++ 程序时将生成大约 1000 个输出。每个输出文件必须有一个不同的名称。

我希望运行的命令的形式为:

program_name -input -output -o1 -o2 -o3

迄今为止我已经尝试过:

import os

cwd = os.getcwd()

files = os.listdir(cwd)

required_files = []

for i in file:
    if i.endswith('.ttp'):
         required_files.append(i)

所以,我有一个必需文件的数组。我的问题 - 如何迭代数组并为每个条目将其作为参数传递给上面的命令 (program_name) 并为每个文件指定唯一的输出 id?

I am currently struggling to call a non python program from a python script.

I have a ~1000 files that when passed through this C++ program will generate ~1000 outputs. Each output file must have a distinct name.

The command I wish to run is of the form:

program_name -input -output -o1 -o2 -o3

To date I have tried:

import os

cwd = os.getcwd()

files = os.listdir(cwd)

required_files = []

for i in file:
    if i.endswith('.ttp'):
         required_files.append(i)

So, I have an array of the neccesary files. My problem - how do I iterate over the array and for each entry, pass it to the above command (program_name) as an argument and specify a unique output id for each file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-09-09 19:16:06

您可以使用 subprocess 来实现此目的:

import os
import subprocess

cwd = os.getcwd()

for i in os.listdir(cwd):
    if i.endswith('.ttp'):
        o = i + "-out"
        p = subprocess.call(["program_name", "-input", i, "-output", o])

You can use subprocess for that purpose:

import os
import subprocess

cwd = os.getcwd()

for i in os.listdir(cwd):
    if i.endswith('.ttp'):
        o = i + "-out"
        p = subprocess.call(["program_name", "-input", i, "-output", o])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文