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

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

Post by Anonymous »

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?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post