#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0

    Intelligent python script


    Hi All,
    I want to know if it is possible to write an intelligent python script which accepts inputs from a text file (which was intended to be given through keyboard) and executes a program.
    Eagerly waiting for reply,
    Shyam
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The answer is yes, but your question is so vague as to be meaningless. What sort of input? What sort of program?

    here is a program that will fulfill your requirements:

    Code:
    import os
    some_text = os.stdin.readline()   #read input from a file or the keyboard
    os.system('aprogram.exe')  #execute a program
    but it does not do anything useful.

    What do you mean by intelligent? Do you mean read in english text and understand it? That will take a little more work.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0

    Intelligent python script


    Thanks for the reply,
    Sorry for the ambiguous question.
    What I meant was-
    Instead of giving input through keyboard(STDIN),I want to give inputs through a file.For example, consider a simple C program used as a calculator.Let it accept a keyboard input and based on that, it performs an operation(add,subtract,mul,div). What I want to do is, instead of giving keyboard input,I want to feed it through a file like "input.txt".The content of this file is:
    Add
    Sub
    Mul
    Div
    Can my excutable (say "calc.exe",the C file being "calc.c" ) take inputs from "input.txt" one by one and perform all the 4 operations one after the other ?How can I do this using python (automating keyboard input) ?
    My python script must be intelligent enough to feed these inputs(add,sub,mul,div)one by one to the executable calc.exe.
    Regards,
    Shyam
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    If the C program reads input from stdin, then you do not need Python to read the inputs from a file - you can do it on the command line by redirecting stdin using '<'.

    e.g.

    C:\> calc.exe < input.txt

    This will execute calc.exe, reading the input from input.txt as if it came from the keyboard. (In the above example, "C:\> is the Windows DOS prompt, but this will work in both DOS and UNIX).


    If you want more control then you can do it in Python using a pipe.
    Code:
    import os
    f = open('input.txt')
    pipe= os.popen('calc.exe', 'w')
    for line in f:
       ...do stuff with line...
       pipe.write(line)
    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo