Python - File Read All

Introduction

To read the entire file into a string all at once with the file object's read method and print it:

Demo

myfile = open('myfile.txt', 'w')        # Open for text output: create/empty 
myfile.write('hello text file\n')       # Write a line of text: string 
myfile.write('goodbye text file\n') 
myfile.close()                          # Flush output buffers to disk 
# from   ww w.j a  va 2 s . c  o m
print( open('myfile.txt').read() )     # Read all at once into string

Result