Access command line arguments as bytes in python3 [duplicate]

爷,独闯天下 提交于 2019-12-11 02:58:20

问题


Is it possible to access the raw argv elements binary content ?

$ python3 -c'import sys;print(sys.argv);' `echo -ne "\xff\x80\x00\xff"`
['-c', '\udcff\udc80\udcff']

回答1:


You can obtain the argv contant as bytes as follows:

#!/usr/bin/python3
import sys
arg1_bytes = sys.argv[1].encode(sys.getfilesystemencoding(), 'surrogateescape')

Source: PEP 383 - Non-decodable Bytes in System Character Interfaces, via answer on "Command-line arguments as bytes instead of strings in python3"; see also answer on "sys.argv as bytes in Python 3k"



来源:https://stackoverflow.com/questions/26503817/access-command-line-arguments-as-bytes-in-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!