Reading and writing text files

Reading from a text file

f = open('mydata.txt', 'r')
  • The open() function returns a file object
    • If the file is not accessible, Python gives an error
  • The second argument, specifies a mode in which the file will be read
    • r = (only for) reading
    • w = (only for) writing
    • a = append (everything you write will go to the end of the text file)
    • r+ = read and write mode

Writing to a text file