Skip to content

Instantly share code, notes, and snippets.

@dougn
Created May 12, 2014 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougn/adf1eff92f8d5e34d17d to your computer and use it in GitHub Desktop.
Save dougn/adf1eff92f8d5e34d17d to your computer and use it in GitHub Desktop.
Python3 cat example
## 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)
@dougn
Copy link
Author

dougn commented May 12, 2014

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.

@dougn
Copy link
Author

dougn commented May 12, 2014

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