I am using open/read functions, there's feof for fopen/fread to detect end-of-file, what's the equivalent way for open/read?
feof() The function feof() is used to check the end of file after EOF. It tests the end of file indicator. It returns non-zero value if successful otherwise, zero.
The read() method of the input stream classes reads the contents of the given file byte by byte and returns the ASCII value of the read byte in integer form. While reading the file if it reaches the end of the file this method returns -1.
Use Walrus Operator to Find End of File in Python It is denoted by := . This operator is basically an assignment operator which is used to assign True values and then immediately print them.
Python doesn't have built-in eof detection function but that functionality is available in two ways: f. read(1) will return b'' if there are no more bytes to read. This works for text as well as binary files. The second way is to use f.
From man read:
On success, the number of bytes read is returned (zero indicates end of file), ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With