开发者

Calling a non python program from python?

开发者 https://www.devze.com 2023-01-01 00:52 出处:网络
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 d

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?


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

精彩评论

暂无评论...
验证码 换一张
取 消