fixup file IO

use the io module so it works with python 3
make sure all file IO is in unicode
use with as everywhere so we actually close files