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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With