Advertisement
rfmonk

codecs_open_write_IndexError_out_of_range.py

Jan 29th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #1/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7.  
  8. from codecs_to_hex import to_hex
  9.  
  10. import codecs
  11. import sys
  12.  
  13. encoding = sys.argv[1]
  14. filename = encoding + '.txt'
  15.  
  16. print 'Writing to', filename
  17. with codecs.open(filename, mode='wt', encoding=encoding) as f:
  18.     f.write(u'pi: \u03c0')
  19.  
  20. # Determine the byte grouping to use for to_hex()
  21. nbytes = {'utf-8': 1,
  22.           'utf-16': 2,
  23.           'utf-32': 4,
  24.           }.get(encoding, 1)
  25.  
  26. # Show the raw bytes in the file
  27. print 'File contents:'
  28. with open(filename, mode='rt') as f:
  29.     print to_hex(f.read(), nbytes)
  30.  
  31. """ Traceback (most recent call last):
  32.    File "codecs_open_write.py", line 13, in <module>
  33.    encoding = sys.argv[1]
  34.    IndexError: list index out of range
  35. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement