Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access sys.argv as bytes in Python 3 [duplicate]

As the title said, is there a sys.argv equivalent in python 3 allow me to read arguments as bytes?

The reason I want this is, I have a script which accept a bytes (\xe9\x88...) as its first arg, the first arg is supposed to be a bytes converted from an utf8 string, sys.argv will try to decode the first arg using some encoding, which may not utf8, so the program may fail. If I can access the args without calling sys.argv, the conversion will not be done.

like image 791
Not an ID Avatar asked Nov 28 '14 09:11

Not an ID


1 Answers

There was an issue on the Python bug tracker about this:

In some situations, the encoding of the command line is incorrect or unknown. sys.argv is decoded with the file system encoding which can be wrong. Eg. see issue #4388 (ok, it's a bug, it should be fixed).

As os.environb, it would be useful to have bytes version of sys.argv to have able to decide the encoding used to decode each argument, or to manipulate bytes if we don't care about the encoding.

Since on Python 3 sys.argv is encoded with the filesystem encoding, the proposed solution on the bug is to use os.fsencode:

argvb = list(map(os.fsencode, sys.argv))

os.fsencode encodes the string using the string using the surrogateescape encoding, which is defined in PEP-383.

like image 180
vz0 Avatar answered Oct 15 '22 07:10

vz0