Skip to content
Advertisement

Python reads incremental text

I saw a piece of code, as follows:

with open(‘text.txt’,‘r’)as fp:
  
    while True:
        data_line=fp.readline()

        if data_line:
            print(data_line)

writing data in txt,and all the time python will keep outputting.

I think this code is great. But I don’t quite understand the line if data_line:

I hope someone can explain it, thanks.

Advertisement

Answer

Basically the code is checking if there is any value in the variable data_line and printing the value if the value is not '' empty string.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement