Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python open which uses a hyphen for stdin and stdout
import sys
def openstd(file, mode='r', *args, **kwargs):
if file == '-':
if 'r' in mode:
return sys.stdin
elif any(w in mode for w in 'wxa'):
return sys.stdout
else:
raise Exception("Mode not supported")
return open(file, mode, *args, **kwargs)
with openstd(sys.argv[1]) as infile, \
openstd(sys.argv[2], 'w') as outfile:
outfile.write("Got " + infile.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.