-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

sirkrp
  • 1
  • 1
  • Possible duplicate of [How do I read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list) – j-i-l Nov 06 '17 at 21:45
  • I would recommend never using `input`, and use `raw_input` instead – Cireo Nov 06 '17 at 21:47

3 Answers3

0

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

theBrainyGeek
  • 584
  • 1
  • 6
  • 17
  • 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 – sirkrp Nov 06 '17 at 22:09
  • 1
    @Karim Take a closer look at that link. It does what you want – Cireo Nov 09 '17 at 01:26
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'
Cireo
  • 4,197
  • 1
  • 19
  • 24
  • 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? – sirkrp Nov 06 '17 at 22:16
  • 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)` – Cireo Nov 09 '17 at 01:24
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]
eyalstoler
  • 184
  • 1
  • 7