Besides the above method, any other way to find out whether is fp is already at the eof? Show asked Apr 13, 2012 at 11:52
1
When reading a file in chunks rather than with
Or, shorter:
answered Apr 13, 2012 at 11:55
Fred FooFred Foo 347k72 gold badges724 silver badges824 bronze badges 9 The "for-else" design is often overlooked. See: Python Docs "Control Flow in Loop": Example
BigMan73 1,04413 silver badges13 bronze badges answered Jul 14, 2014 at 14:18
BeepBoopBeepBoop 1,21410 silver badges12 bronze badges 2 I'd argue that reading from the file is the most reliable way to establish whether it contains more data. It could be a pipe, or another process might be appending data to the file etc. If you know that's not an issue, you could use something like:
answered Apr 13, 2012 at 11:54
NPENPE 471k104 gold badges922 silver badges997 bronze badges 3 As python returns empty string on EOF, and not "EOF" itself, you can just check the code for it, written here
joanis 8,23112 gold badges26 silver badges36 bronze badges answered Jun 16, 2016 at 9:51
tingtongtingtong 2552 silver badges2 bronze badges 5 When doing binary I/O the following method is useful:
The advantage is that sometimes you are processing a binary stream and do not know in advance how much you will need to read. answered Aug 1, 2014 at 1:39
user545424user545424 15.3k11 gold badges53 silver badges69 bronze badges 11 You can compare the returned value of Furthermore, I don't think your example code actually works. The answered Apr 13, 2012 at 11:54
1 read returns an empty string when EOF is encountered. Docs are here. answered Apr 13, 2012 at 11:55
0110011001100110 2,2363 gold badges22 silver badges32 bronze badges
NorthCat 9,17716 gold badges45 silver badges49 bronze badges answered Dec 9, 2014 at 7:08
sambasamba 6693 gold badges10 silver badges19 bronze badges 2 I really don't understand why python still doesn't have such a function. I also don't agree to use the following
The main reason is The method works for me is like the following. If you have some pseudocode like the following
You can replace it with:
This method is simple and you don't need to change most of you code. answered Feb 8, 2018 at 7:20
Han LuoHan Luo 1883 silver badges4 bronze badges If file is opened in non-block mode, returning less bytes than expected does not mean it's at eof, I'd say @NPE's answer is the most reliable way:
answered Sep 10, 2013 at 4:37
ymattwymattw 1,09910 silver badges7 bronze badges The Python read functions will return an empty string if they reach EOF answered Apr 13, 2012 at 11:55
mensimensi 9,4102 gold badges33 silver badges43 bronze badges
You can use the file methods seek() and tell() to determine the position of the end of file. Once the position is found, seek back to the file beginning answered Jul 31, 2017 at 8:57
1 Python doesn't have built-in eof detection function but that functionality is available in two ways: Below are both implementations. Using tell() method
Using read() method
How to use this
Play with this code. answered May 1, 2019 at 0:47
Shital ShahShital Shah 58.1k12 gold badges224 silver badges180 bronze badges 1 You can use
progmatico 4,3341 gold badge14 silver badges25 bronze badges answered Jan 27, 2018 at 21:47
wambawamba 111 bronze badge 1 Reading a file in batches of
answered Feb 25, 2018 at 22:33
boechat107boechat107 1,52413 silver badges23 bronze badges 1 Get the EOF position of the file:
and compare it with the current position: answered Jul 22, 2018 at 19:11
Константин ВанКонстантин Ван 10.8k7 gold badges54 silver badges65 bronze badges Although I would personally use a Use a try-catch with
answered Jan 12, 2016 at 4:19
Blairg23Blairg23 10.5k6 gold badges70 silver badges70 bronze badges I use this function:
answered Jul 31, 2016 at 22:39
1
This code will work for python 3 and above
answered Aug 4, 2020 at 6:17
You can try this code:
answered Jan 8 at 1:44
1 You can use below code snippet to read line by line, till end of file:
Assem 11k5 gold badges53 silver badges92 bronze badges answered Oct 27, 2014 at 12:11
A RA R 2,5673 gold badges19 silver badges37 bronze badges What does readline return at end of file Python?readline() returns the next line of the file which contains a newline character in the end.
How do I read a file from the bottom?Open the file straight to the end ( +G ) with less +G path/to/filename .. Up Arrow = scroll one line up.. Down Arrow = scroll one line down.. u = scroll a half page up.. d = scroll a half page down.. PageUp = scroll a full page up.. PageDown = scroll a full page down.. Can we use EOF in Python?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.
How do you read until the end of a line in Python?The readlines () method is the most popular method for reading all the lines of the file at once. This method reads the file until EOF (End of file), which means it reads from the first line until the last line. When you apply this method on a file to read its lines, it returns a list.
|