Week 7 - How to Handle Files in Python

  1. open(‘filename.txt’,’r/r+/a/w/w+/x’)
  2. File attributes:
    • .readline(): Read only one line of the file
    • .readlines(): Read multiple lines of the file as a list of strings
    • .write(): Open an existing file and write to it
    • .read(): loads the content of the file into memory as a string, including formatting such as new line (\n)
    • .read(n): reading a file where n = number of characters to read
    • seek(index): go back to indicated line
  3. .close(): open file takes memory so we can use the .close() method to free resources.
Read More