Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make python keep reading lines until a condition is met? [duplicate]

Tags:

python

This is my current code:

StockSearch = input("Search For Product (Name Or Code):\n")
with io.open('/home/jake/Projects/Stock','r', encoding='utf8') as f:
    for line in f:
        Read = f.readline()
        while Read!='':
            if StockSearch == Read:      
                print (Read)

I'm trying to get python to keep reading every line of the file until there are none left and match the user's input with one of the lines, if not it'll print an error code.

Thank you in advance.


1 Answers

StockSearch = input("Search For Product (Name Or Code):\n")
found = False
with io.open('/home/jake/Projects/Stock','r', encoding='utf8') as f:
    for line in f:
        line = line.rstrip('\n')
        if StockSearch == line:      
            print(line)
            found = True
            break
if not found:
    print("Nothing found")
like image 168
DAXaholic Avatar answered May 14 '26 18:05

DAXaholic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!