What's the difference between volatile and non-volatile memory, and what are some examples of each? →
volatile memory - memory that requires an electrical current to maintain state; for example, your computer's main memory/RAM
non-volatile memory - memory that can maintain state without power; for example, SSDs, hard drives, DVDs, etc.
Storing Data in Main Memory
What are the consequences of your data being stored in your computer's main memory? →
data may go away at the end of the program, or when the computer gets turned off
if you're working with large amounts of data, you may run out of RAM (which is typically less than the amount of non-volatile memory that you have)!
So, if you want to persist data beyond the lifetime of your running program or through on-off cycles… →
Store data as a file on non-volatile memory.
open
In Python, what built-in function do we use to interact with files? How many parameters does it have, and what does it return? →
open opens a file for reading or writing
it takes two arguments: a filename and a mode
filename - the absolute or relative path from your program to the file you're opening
mode - can be one of the following:
'w' - write
'r' - read
'a' - append
it returns a file object
A File Object …
is an object that allows your program to manipulate/read/write to an actual file on disk
to create a file object and open a file, use the built-in function, open()
Putting Data Into a File
What are the steps for opening a file and putting data into it? What file object method is used for putting data into a file? →
Writing to a File
Get a file object using open with write mode: open('filename', 'w')
filename is the file to be opened
'w' means that the file will be opened for writing
if the file doesn't exist, 'w' will create it
if the file exists, 'w' will overwrite it!
Use the write method on the file object to write data to the file
takes a string as an argument (non-string will give a TypeError)
does not automatically add new lines
Use the close method on the file object when you're done
Writing to a File Code
Retrieving Data From a File
What are the steps for opening a file and retrieving data from it? What file object methods can be used for reading data from a file? →
Reading a File
Get a file object using open with read mode: open('filename', 'r')
filename is the file to be opened
'r' means that the file will be opened for reading
To read data…
iterate over the file object itself (reads one line at a time)
use one of the following methods:
iterate over the file object itself (use a for loop with the file object)
readline()
read()
readlines()
Use the close method on the file object when you're done
Methods for Reading a File
All of the following methods do not have any parameters.
readline() - reads in a single line (with newline at the end of each line)
read() - reads in entire contents of a file as a string
readlines() - reads in entire contents of a file as a list of lines
Examples
The following examples assume the presence of a file called ingredients.txt (download here - right-click and save as) in the same folder/directory as your program.