4

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?

3
  • can you not specify the output file in your command call? Commented May 27, 2010 at 9:11
  • How are the input file names passed to the program? I don't really understand your example, is "-input" supposed to be the file name, or is it supposed to follow after "-input" (and omitted)? Commented May 27, 2010 at 9:16
  • Ah, sorry - the file name occurs after input and output, i.e., -input [fname.in] -output[fname.out]. Commented May 27, 2010 at 9:18

1 Answer 1

11

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])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Luper - you have saved me hours!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.