1

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.

Youyou Hua
  • 49
  • 5
  • 1
    Does this answer your question? [What is Truthy and Falsy? How is it different from True and False?](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false) – Pranav Hosangadi Jun 22 '21 at 17:27

2 Answers2

1

if data_line checks if any text was read: if it is an empty string it will skip that section (the print(data_line))

n4321d
  • 1,059
  • 2
  • 12
  • 31
1

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.

Debashis Dip
  • 91
  • 1
  • 6