myfile close(myfile open('myfile txt'myfile readline('hello text file\nmyfile readline('goodbye text file\nmyfile readline('flush output buffers to disk open for text input'ris default read the lines back empty stringend-of-file notice that file write calls return the number of characters written in python xin they don'tso you won' see these numbers echoed interactively this example writes each line of textincluding its end-of-line terminator\nas stringwrite methods don' add the end-of-line character for usso we must include it to properly terminate our lines (otherwise the next write will simply extend the current line in the fileif you want to display the file' content with end-of-line characters interpretedread the entire file into string all at once with the file object' read method and print itopen('myfile txt'read('hello text file\ngoodbye text file\nread all at once into string print(open('myfile txt'read()hello text file goodbye text file user-friendly display and if you want to scan text file line by linefile iterators are often your best optionfor line in open('myfile txt')print(lineend=''hello text file goodbye text file use file iteratorsnot reads when coded this waythe temporary file object created by open will automatically read and return one line on each loop iteration this form is usually easiest to codegood on memory useand may be faster than some other options (depending on many variablesof coursesince we haven' reached statements or iterators yetthoughyou'll have to wait until for more complete explanation of this code windows usersas mentioned in open accepts unix-style forward slashes in place of backward slashes on windowsso any of the following forms work for directory paths--raw stringsforward slashesor doubled-up backslashesopen( ' :\python \lib\pdb py'readline('#/usr/bin/env python \nopen(' :/python /lib/pdb py'readline('#/usr/bin/env python \ tuplesfilesand everything else