1

I have a python script say script1.py. It will prompt the user with a serious of questions like Name, 'Y' / 'N' type questions. Now i need to call this python script from another python script,say scripts2.py such that I would define the user inputs in script2.py. So how to pass the input sequentially???

Help would be appreciated.

Regards, Sujith

2 Answers 2

1

Sounds like you have a stand-alone program that reads from stdin, and you want to automate input to it using python. Download and use the pexpect module, that's what it's for.

Sign up to request clarification or add additional context in comments.

1 Comment

I used the expect module for this and finally arraived at a solution. Thanks :)
0

Make script1.py a module that will have a class of questions. In script2.py set up an object of script1 and call the questions... something like this:

In script2.py

from script1 import Questions
answer = raw_input(Questions.question1())

In script1.py

class Questions:
    def question1():
        return "What is your name? (Y/N)"

7 Comments

I aint very clear abt ur suggestion. But I am not allowed to make any changes in the script1.py. And I don have any class in script1. Answers much appreciated.
So script1.py is already written? If so, then post it. I figured nothing was written yet. Posting what you already have will help members find solutions for you more effectively and efficiently.
I am really sorry abt that. I am not in a position to put the exact code. But I could give an overview of wat it does. Basically we prompt the user with if need to proceed with questions ('Y'/'N') If 'Y' then he ld be asked a question and its answer would be captured in a file for future reference. If 'N' he would asked if we can quit and then we quit.Its the functionality of the scripts1. Now I need is script2 where I would call the script1 which would obviously shoot out the questions.In script2 I can define sumewere like 'Y','Name,'Y','Age,'N'
In script2 I can define sumewere like 'Y','Name,'Y','Age,'N' ('Y' accepting the question and next is the answer for that and 'N' for quit) The save functionality would be better taken care by script1. I just need a script2 to pass this inputs automatically.
Yeah, without seeing the code, and trying to understand what you are describing this may be difficult. But if I understand you correctly then the following should help. Script1.py is already written and functional, so you need to look at it and more than likely it will have definitions (functions) that you can call and they will return something, just like the example I already posted. You just need to call those functions, and store the user input into a variable. Then take that variable and make a conditional statement saying "if Y then ask another question, else if N then stop".
|

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.