|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Intelligent python script |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|