Search code examples
pythonraw-input

python paste multilines to raw_input in a while loop


I have some code looks like this:

while line != '' and line != 'STOP ME':
    line = raw_input("")
    buf.append(line+'\n')

sys.stdin.flush()
print raw_input("Input1:")
print raw_input("Input2:")

The problem is, if the user copy and paste the data like this

line1

line2

STOP ME

"empty line"

"empty line"

Some junk text

My raw_input will be overflowed with junk text after "STOP ME". Is there a way to avoid it?


Solution

  • May be you don't need raw_input here? You can use sys.stdin file-like object. For example sys.stdin.read method.

    Or you can use somthing like this:

    buf.append(line.split('STOP ME\n', 1)[0]+'\n')
    

    to get data before STOP ME line.