How an entire file is read into buffer and returned as a string in Python?



Python's File class' read function automatically manages to do that. When you open a file in python and call the read function on the file handle, it will read the whole file in a string and return that string. 

 example

with open('my_file.txt', 'r') as f:
    file_content = f.read() # Read whole file in the file_content string
print(file_content)

Advertisements