As Python 3k introduces strict distinction between strings and bytes, command line arguments in the array sys.argv are presented as strings. Sometimes it is necessary to treat the arguments as bytes, e.g. when passing a path that needn't to be in any particular character encoding in Unix.
Let's see an example. A brief Python 3k program argv.py follows:
import sys
print(sys.argv[1])
print(b'bytes')
When it is executed as python3.1 argv.py français it produces expected output:
français
b'bytes'
Note that the argument français is in my locale encoding. However, when we pass the argument in a different encoding we obtain an error: python3.1 argv.py `echo français|iconv -t latin1`
Traceback (most recent call last):
File "argv.py", line 3, in <module>
print(sys.argv[1])
UnicodeEncodeError: 'utf-8' codec can't encode character '\udce7' in position 4: surrogates not allowed
How shall we pass binary data to Python 3k program via command line arguments? An example of usage is passing a path to a file of a user who uses other locale.
python3 argv.py `echo -ne "\xff\x80\x00"`which is an example of « passing binary data via command line arguments »