Created
October 8, 2019 04:12
-
-
Save louisswarren/292e125013e73a9683aa422ab95bd97a to your computer and use it in GitHub Desktop.
Python open which uses a hyphen for stdin and stdout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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