Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1/usr/bin/env python
- # this is from The Python
- # Standard Library by example
- # ISBN13: 9780321767349
- import codecs
- import sys
- encoding = sys.argv[1]
- filename = encoding + '.txt'
- print 'Reading from', filename
- with codecs.open(filename, mode='rt', encoding=encoding) as f:
- print repr(f.read())
- """ Errors
- $ python codecs_open_read.py utf-8
- Reading from utf-8.txt
- u'pi: \u03c0'
- $ python codecs_open_read.py utf-16
- Reading from utf-16.txt
- Traceback (most recent call last):
- File "codecs_open_read.py", line 14, in <module>
- with codecs.open(filename, mode='rt', encoding=encoding) as f:
- File "/usr/lib/python2.7/codecs.py", line 881, in open
- file = __builtin__.open(filename, mode, buffering)
- IOError: [Errno 2] No such file or directory: 'utf-16.txt'
- $ python codecs_open_read.py utf-32
- Reading from utf-32.txt
- Traceback (most recent call last):
- File "codecs_open_read.py", line 14, in <module>
- with codecs.open(filename, mode='rt', encoding=encoding) as f:
- File "/usr/lib/python2.7/codecs.py", line 881, in open
- file = __builtin__.open(filename, mode, buffering)
- IOError: [Errno 2] No such file or directory: 'utf-32.txt'
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement