tags: 📥️/📰/🟥️ publish: true aliases: - Python — Best Practices for File Operations cover: '![rw-book-cover](https://readwise-assets.s3.amazonaws.com/static/images/article3.5c705a01b476.png)' general_subject: specific_subject: source: medium isbn: doi: url: 'https://medium.com/p/a8391f13dbe2' author: "[[@Tony]]" guest: publish_date: reviewed_date: --- ![rw-book-cover](https://readwise-assets.s3.amazonaws.com/static/images/article3.5c705a01b476.png) ## Highlights - To solve this problem, we need to put this “standard practice” aside for now and use a more low-level file.read() approach. Instead of iterating over the file object in a loop, each call file.read(chunk_size) will directly return the chunk_size file content of the size read from the current position, without waiting for any newline characters to appear. - Almost everyone knows that there is a “standard practice” for reading files in Python: first get a file object using a with open(fine_name) context manager, and then for iterate over it with a loop, getting the contents of the file line by line. For example: - def count_nine(fname): count = 0 with open(fname) as file: for line in file: count += line.count('9') return count - def count_nine_v2(fname): """Count total 9s,read 8kb each time """ count = 0 block_size = 1024 * 8 with open(fname) as fp: while True: chunk = fp.read(block_size) # If no more content if not chunk: break count += chunk.count('9') return count