2

I need to run a simple C program several time, each one with different input string (let's say AAAAA... increasing size, until I get "TRUE" as output). e.g.

./program A # output FALSE
./program AA # output FALSE
./program AAA # output FALSE
./program AAAA # output FALSE
./program AAAAA # output FALSE
./program AAAAAA # output FALSE
./program AAAAAAA  # output TRUE

in C I would simply use a while loop. I know in python there is the while loop as well.

So the python program would be:

strlen = 0
while TRUE
 strlen++
 <run ./**C program** "A"*strlen > 
 if (<program_output> = TRUE)
   break

Given that I can make the .py script executable by writing

#! /usr/bin/env python

and

 chmod +x file.py

What should I do to make this work?

Thanks in advance

2
  • ...that's not a Python program Commented Mar 5, 2014 at 17:32
  • I know, the syntax is not the right one. it's just an example Commented Mar 5, 2014 at 17:33

4 Answers 4

4

You can use subprocess.check_output:

import subprocess
strlen = 0
while True:
    strlen += 1
    if subprocess.check_output(['./program', 'A'*strlen]) == 'TRUE':
        break
Sign up to request clarification or add additional context in comments.

3 Comments

I tried the code. It seems to work.. but after 1 loop only it gets stuck. I tried to place print(strlen) after strlen +=1 and indeed it prints 1 only and nothing more (nor the program exits)
actually it doesn't. he program (./program) after every execution requires a user interrupt (ctrl+C) to stop. And obviously if I do it, the program ./program exists, as well as the .py program. is there any one to make ./program exit, but the .py continue?
You can set a timeout argument to check_output (see the doc linked above). A cleaner way would be to use subprocess.Popen directly (check_output is a wrapper), read the four first characters from the process' stdout, and then kill it.
2

You could try something like this (see docs):

import subprocess

args = ""
while True:
     args += "A"
     result = subprocess.call(["./program", "{args}".format(args=args)])
     if result == 'TRUE':
         break

The subprocess module is preferred to the os.popen command, since it has been "deprecated since version 2.6." See the os documentation.

3 Comments

program is not a .py script, but a C program
the code works. why do you use "args"? However, the program (./program) after every execution requires a user interrupt (ctrl+C) to stop. And obviously if I do it, the program ./program exists, as well as the .py program. is there any one to make ./program exit, but the .py continue?
I was using 'args' to refer to the command-line arguments you would be passing in, had you simply ran your c file from command line. You could call that variable whatever you want. And you might be able to do a result.kill() with the revised subprocess.Popen(...)
1

file.py

import os

count=10
input="A"
for i in range(0, count):
    input_args=input_args+input_args
    os.popen("./program "+input_args)

running file.py would execute ./program 10 times with increasing A input

2 Comments

program is not a .py script, but a C program
@dragonmnl made changes to run program as executable
1

Use commands. Here is the documentation http://docs.python.org/2/library/commands.html

  1. commands.getstatusoutput returns a stdout output from your C program. So, if your program prints something, use that. (In fact, it returns a tuple (0, out) for stdout).
  2. commands.getstatus returns boolean status from program which you can use as well.

So, assuming you are using stdout to capture the ./program output, the entire modified program looks like

import commands

while TRUE:
  strlen += 1
  output = commands.getstatusoutput("./program " + "A"*strlen)
  outstatus = output[1]
  if output == "true":
     break

I would experiment with getstatus to see if I can read values returned by program.

Edit: Didn't notice that commands is deprecated since 2.6 Please use subprocess as shown in other response.

2 Comments

Don't use commands. Use the subprocess module instead. The big fat warning at the top of the documentation for commands tells you: Deprecated since version 2.6.
Oops.. Didn't notice the deprecated part. I used it sometime back, not recently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.