-2

So I have a text file that should be used as user input throughout my entire Python script. Let's say that my text file looks like this:

input1
input2
input3
input4

And then I have a while loop that looks like this:

mainFlag = True
while mainFlag == True:
    userInput = input("Choose one of the 4 inputs")
    if userInput == 'input1':
        function1()
    elif userInput == 'input2':
        function2()
    elif userInput == 'input3':
        function3()
    elif userInput == 'input4':
        function4()

How can I loop through the input text file, take each line as a string and use that string as user input for inside the while loop?

Thanks

2
  • Possible duplicate of How do I read a file line-by-line into a list? Commented Nov 6, 2017 at 21:45
  • I would recommend never using input, and use raw_input instead Commented Nov 6, 2017 at 21:47

3 Answers 3

0

You should have looked around before asking this question. You just need to use readline(). Python: read all text file lines in loop

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

2 Comments

You should have read my question before answering. I am looking for a way to loop through the text file and take each line as an input to a separate block of code
@Karim Take a closer look at that link. It does what you want
0

I would recommend using the fileinput module (https://docs.python.org/2/library/fileinput.html) instead, but it is worth mentioning that you can just pipe input into a program that expects to read from the user, e.g.:

bash-3.2$ cat prog.py 
#!/usr/bin/env python

while True:
    try:
        x = raw_input()
    except EOFError:
        break
    if x == "a":
        print 'got a'
    elif x == 'b':
        print 'such b'
    else:
        print 'meh %r' % x
bash-3.2$ cat vals.txt 
a
b
c
bash-3.2$ # equivalent to: cat vals.txt | ./prog.py
bash-3.2$ ./prog.py < vals.txt
got a
such b
meh 'c'

2 Comments

Thank you for your answer. I am looking for a way to do this without involving any bash scripts. My thinking was to read the text file into a list so that I have a list of inputs and then loop through this list and in each iteration somehow use the list element as user input. How can I do that?
Don't use input or raw_input then. They are for reading from stdin, and it will "require" (in a sane world) you to use a shell to send your file into stdin. Just use normal file manipulation: with open(PATH) as f: for line in f: do_stuff(line)
0

What you are looking for sounds like a classic generator solution (read pep 255 for more info):

def function1():
    print("function1")

def function2():
    print("function2")

def function3():
    print("function3")

def choose_input(the_input):
    return {
        'input1': function1,
        'input2': function2,
        'input3': function3
    }[the_input]


with open("file.txt") as file:
    inputs = (choose_input(i.rstrip("\n")) for i in file.readlines())

[my_input_function_call() for my_input_function_call in inputs]

Comments

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.