Added "with open()" to main loop

Replaced argparse file-opening functionality using "with open()" to more correctly handle file open/write/close and avoid error states which could lock files open.