""" Exercise 5.16 from "A primer on...". The exercise does not ask for a function, but it is convenient to write one that can be reused in Ex. 5.18. """ import sys import matplotlib.pyplot as plt def read_density_file(filename): temp = [] dens = [] with open(filename) as infile: for line in infile: if line[0] == '#' or line.isspace(): continue #continue to next loop iteration (do nothing) else: words = line.split() temp.append(float(words[0])) dens.append(float(words[1])) return temp, dens if __name__ == '__main__': """ Because of the if-test above, the following code is only run if this file is the main program (i.e. run on its own), not if it is imported as a module into another program. """ density_file = sys.argv[1] temp, dens = read_density_file(density_file) plt.plot(temp, dens,'o') plt.show() """ Terminal> python read_density_data.py density_air.txt (output is a plot) Terminal> python read_density_data.py density_water.txt (output is a plot) """