Checking eof in python

Checking for EOF in stream

Classic situation — I have to process an input stream of unknown length
until a I reach its end (EOF, End Of File). How do I check for EOF? The
input stream can be anything from opened file through sys.stdin to a
network socket. And it’s binary and potentially huge (gigabytes), thus
«for line in stream.readlines()» isn’t really a way to go.

stream = sys.stdin
while True:
data = stream.read(1024)
process_data(data)
if len(data) < 1024: ## (*)
break

I smell a fragile point at (*) because as far as I know e.g. network
sockets streams may return less data than requested even when the socket
is still open.

I’d better like something like:

but there is not eof() method 🙁

This is probably a trivial problem but I haven’t found a decent solution.

Classic situation — I have to process an input stream of unknown length
until a I reach its end (EOF, End Of File). How do I check for EOF? The
input stream can be anything from opened file through sys.stdin to a
network socket. And it’s binary and potentially huge (gigabytes), thus
«for line in stream.readlines()» isn’t really a way to go.

stream = sys.stdin
while True:
data = stream.read(1024)


Grant Edwards grante Yow! CALIFORNIA is where
at people from IOWA or NEW
visi.com YORK go to subscribe to
CABLE TELEVISION!!

Classic situation — I have to process an input stream of unknown length
until a I reach its end (EOF, End Of File). How do I check for EOF? The
input stream can be anything from opened file through sys.stdin to a
network socket. And it’s binary and potentially huge (gigabytes), thus
«for line in stream.readlines()» isn’t really a way to go.

stream = sys.stdin
while True:
data = stream.read(1024)

Right, not a big difference though. Isn’t there a cleaner / more
intuitive way? Like using some wrapper objects around the streams or
something?

Grant Edwards wrote: >On 2007-02-19, GiBo >>
Classic situation — I have to process an input stream of unknown length
until a I reach its end (EOF, End Of File). How do I check for EOF? The
input stream can be anything from opened file through sys.stdin to a
network socket. And it’s binary and potentially huge (gigabytes), thus
«for line in stream.readlines()» isn’t really a way to go.

stream = sys.stdin
while True:
data = stream.read(1024)

Right, not a big difference though. Isn’t there a cleaner / more
intuitive way? Like using some wrapper objects around the streams or
something? Read the documentation. For a true file object:
read([size]) . An empty string is returned when EOF is encountered
immediately.
All the other «file-like» objects (like StringIO, socket.makefile, etc)
maintain this behavior.
So this is the way to check for EOF. If you don’t like how it was spelled,
try this:

If your data is made of lines of text, you can use the file as its own
iterator, yielding lines:

for line in stream:
process_line(line)

Right, not a big difference though. Isn’t there a cleaner /
more intuitive way? A file is at EOF when read() returns ». The above is the
cleanest, simplest, most direct way to do what you specified.
Everybody does it that way, and everybody recognizes what’s
being done.

It’s also the «standard, Pythonic» way to do it.

You can do that, but then you’re mostly just obfuscating
things.


Grant Edwards grante Yow! Vote for ME
at — I’m well-tapered,
visi.com half-cocked, ill-conceived
and TAX-DEFERRED!

So this is the way to check for EOF. If you don’t like how it was spelled,
try this:

On 2/19/07, Gabriel Genellina Grant Edwards wrote:
>On 2007-02-19, GiBo >>>
>>Classic situation — I have to process an input stream of unknown length
>>until a I reach its end (EOF, End Of File). How do I check for EOF?The
>>input stream can be anything from opened file through sys.stdin to a
>>network socket. And it’s binary and potentially huge (gigabytes), thus
>>»for line in stream.readlines()» isn’t really a way to go.
>>>
>>For now I have roughly:
>>>
>>stream = sys.stdin
>>while True:
>> data = stream.read(1024)
> if len(data) == 0:
> break #EOF
>> process_data(data)
>
Right, not a big difference though. Isn’t there a cleaner / more
intuitive way? Like using some wrapper objects around the streams or
something?

Read the documentation. For a true file object:
read([size]) . An empty string is returned when EOF is encountered
immediately.
All the other «file-like» objects (like StringIO, socket.makefile, etc)
maintain this behavior.
So this is the way to check for EOF. If you don’t like how it was spelled,
try this:

If your data is made of lines of text, you can use the file as its own
iterator, yielding lines:

for line in stream:
process_line(line)

data = f.read(bufsize):
while data:
# . process data.
data = f.read(bufsize)
-The only annoying bit it the duplicated line. I find I often follow
this pattern, and I realize python doesn’t plan to have any sort of
do-while construct, but even still I prefer this idiom. What’s the
concensus here?

What about creating a standard binary-file iterator:

def blocks_of(infile, bufsize = 1024):
data = infile.read(bufsize)
if data:
yield data
-the use would look like this:

for block in blocks_of(myfile, bufsize = 2**16):
process_data(block) # len(block)
(ahem), make that iterator something that works, like:

def blocks_of(infile, bufsize = 1024):
data = infile.read(bufsize)
while data:
yield data
data = infile.read(bufsize)

Источник

How to Check if it is the End of File in Python

If the end of the file (EOF) is reached in Python the data returned from a read attempt will be an empty string.

Let’s try out two different ways of checking whether it is the end of the file in Python.

Calling the Read Method Again

We can call the Python .read() method again on the file and if the result is an empty string the read operation is at EOF.

Some content. Another line of content. 
open_file = open("file.txt", "r") text = open_file.read() eof = open_file.read() if eof == '': print('EOF') 

Read Each Line in a while Loop

Another option is to read each line of the file by calling the Python readline() function in a while loop. When an empty string is returned we will know it is the end of the file and we can perform some operation before ending the while loop.

Some content. Another line of content. 
path = 'file.txt' file = open(path, 'r') x = True while x: line = file.readline() if not line: print('EOF') x = False file.close() 

Источник

Python End of File

Python End of File

  1. Use file.read() to Find End of File in Python
  2. Use the readline() Method With a while Loop to Find End of File in Python
  3. Use Walrus Operator to Find End of File in Python

EOF stands for End Of File . This is the point in the program where the user cannot read the data anymore. It means that the program reads the whole file till the end. Also, when the EOF or end of the file is reached, empty strings are returned as the output. So, a user needs to know whether a file is at its EOF.

This tutorial introduces different ways to find out whether a file is at its EOF in Python.

Use file.read() to Find End of File in Python

The file.read() method is a built-in Python function used to read the contents of a given file. If the file.read() method returns an empty string as an output, which means that the file has reached its EOF.

with open("randomfile.txt", "r") as f:  while True:  file_eof = file_open.read()  if file_eof == '':  print('End Of File')  break 

Note that when we call the open() function to open the file at the starting of the program, we use «r» as the mode to read the file only. Finally, we use the if conditional statement to check the returned output at the end is an empty string.

Use the readline() Method With a while Loop to Find End of File in Python

The file.readline() method is another built-in Python function to read one complete text file line.

The while loop in Python is a loop that iterates the given condition in a code block till the time the given condition is true. This loop is used when the number of iterations is not known beforehand.

Using the while loop with the readline() method helps read lines in the given text file repeatedly.

file_path = 'randomfile.txt'  file_text = open(file_path, "r")  a = True  while a:  file_line = file_text.readline()  if not file_line:  print("End Of File")  a = False  file_text.close() 

The while loop will stop iterating when there will be no text left in the text file for the readline() method to read.

Use Walrus Operator to Find End of File in Python

The Walrus operator is a new operator in Python 3.8. It is denoted by := . This operator is basically an assignment operator which is used to assign True values and then immediately print them.

file = open("randomfile.txt", "r")  while (f := file.read()):  process(f)  file.close() 

Here, the True values are the characters that the read() function will read from the text file. That means it will stop printing once the file is finished.

Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their real-world applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.

Related Article — Python File

Источник

Читайте также:  Author
Оцените статью