Home Code How to read a large text file character by character in Python?

How to read a large text file character by character in Python?

Every python programmer knows how to read a text file line by line without loading the entire file into memory using the below simple script.

with open('text_file.txt') as fl:
    for line in fl:

But what if you want to load a text file character by character? Possible use case is when the text file is a huge file with all its contents in a single line without any newline or line breaking character.

So what if the text file is in a single large line, and the size of the file does not fit into your system’s RAM or memory? In that case you will have to read the text file one character at a time using the Python script below.

with open('text_file_with_single_large_line.txt') as fl:
    while True:
        #read a huge chunk of characters. 
        #Increase this if you want to read more characters in every single read

        #no more pending characters in file?
        if not chars:break

        #process the data
Content Protection by DMCA.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.


Please enter your comment!
Please enter your name here


Subscribe to HitXP Articles

To be updated with all the latest articles, offers and special announcements.

Latest Articles

An Efficient Strategy to fight Corona Virus

By setting up local police station level online delivery networks for essential items like grocery, medicines - the need for general public to come out of their homes can be removed.

Rising pollution can cause Pandemics deadlier than Corona Virus

Rising pollution can expose ancient killer microbes that may cause pandemics more deadlier than Corona. Melting Ice Sheets can resurrect killer agents of the past.

Corona Virus, SARS – Why are new viruses increasingly originating from China?

The reason why new strains of deadly viruses are emerging from China. Viruses like coronavirus and SARS should be stopped from evolving and spreading to prevent another global pandemic.

Evolution of life – primitive cells, complex organisms, intelligence, what next after humans?

Evolution of life from primitive life forms to complex multi cellular organisms to intelligent human species - the advent of AI raises a natural question - What next after human?
Content Protection by DMCA.com