从 python 调用非 python 程序?

编程入门 行业动态 更新时间:2024-10-28 14:24:43
本文介绍了从 python 调用非 python 程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我目前正在努力从 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:16:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!