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: do_some_processing(line)
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 chars=infile.read(1000000) #no more pending characters in file? if not chars:break #process the data do_some_processing(chars)