#!/usr/bin/env python import sys from argparse import ArgumentParser # Set up options parser=ArgumentParser(description="Sponge file before writing output: grep foo file.txt | sponge file.txt", epilog="This tool is a modification from http://joeyh.name/code/moreutils/ with the same name.") parser.add_argument("-a",action="store_true",dest="append",default=False, help="Append to file instead of overwriting") parser.add_argument(type=str,dest="file", help="File to write to") options=parser.parse_args() # Read data data=sys.stdin.read() # choose mode mode="w" if options.append: mode="a" # Write the file writer=open(options.file, mode) writer.write(data) writer.close()