Created
May 12, 2014 18:48
-
-
Save dougn/adf1eff92f8d5e34d17d to your computer and use it in GitHub Desktop.
Python3 cat example
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
## in answer to Armin Ronacher's http://lucumr.pocoo.org/2014/5/12/everything-about-unicode/ | |
import sys | |
import shutil | |
for filename in sys.argv[1:]: | |
f = sys.stdin.buffer | |
if filename != '-': | |
try: | |
f = open(filename, 'rb') | |
except IOError as err: | |
print >> sys.stderr, 'cat.py: %s: %s' % (filename, err) | |
continue | |
with f: | |
shutil.copyfileobj(f, sys.stdout.buffer) | |
PPS: I am NOT saying that Python3 does not have HUGE problems with unicode. Just that this example is no where near as good as all the other past problems Armin has pointed out.
The point of this is to help people running into the same problem, with a sane workaround.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: there are issues with threading and the milti-level buffering at the higher level (sys.stdin/sys.stdout) when doing this. proper flushing should be used before and after talking to the stdin/stdout buffer attribute, and locking in threaded applications.