Wie ist die idiomatische Art, über eine binäre Datei zu iterieren?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie ist die idiomatische Art, über eine binäre Datei zu iterieren?

by Anonymous » 03 Mar 2025, 00:43

Mit einer Textdatei kann ich Folgendes schreiben: < /p>

Code: Select all

with open(path, 'r') as file:
for line in file:
# handle the line
< /code>
Dies entspricht diesem: < /p>
with open(path, 'r') as file:
for line in iter(file.readline, ''):
# handle the line
< /code>
Dieses Idiom ist in PEP 234 dokumentiert, aber ich habe es nicht geschafft, ein ähnliches Idiom für binäre Dateien zu finden.with open(path, 'rb') as file:
while True:
chunk = file.read(1024 * 64)
if not chunk:
break
# handle the chunk
< /code>
Ich habe dasselbe Idiom mit einer Textdatei ausprobiert: < /p>
def make_read(file, size):
def read():
return file.read(size)
return read

with open(path, 'rb') as file:
for chunk in iter(make_read(file, 1024 * 64), b''):
# handle the chunk
Ist es die idiomatische Art, eine binäre Datei in Python zu iterieren?

Top