Moved to sys.stdout.write, not print with rstrip, for editing each
line--that way we avoid the single newline at the end of the file.