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 
#  www. j  av  a  2s .c  o  m
print( open('myfile.txt').read() )     # Read all at once into string

Result