Python - File Read All


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


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   w ww . j av a2  s . co m
print( open('myfile.txt').read() )     # Read all at once into string