从 python 调用非 python 程序? [英] Calling a non python program from python?

查看:31
本文介绍了从 python 调用非 python 程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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)

所以,我有一系列必要的文件.我的问题 - 如何遍历数组并为每个条目将其作为参数传递给上述命令 (program_name) 并为每个文件指定唯一的输出 ID?

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?

推荐答案

您可以使用 子流程为此目的:

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])

这篇关于从 python 调用非 python 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆