Access sys.argv as bytes in Python 3

0 votes
Is there a sys.argv equivalent in python 3 that will let me read arguments as bytes?

 I have a script which accepts a bytes as its first arg, the first arg is supposed to be 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.

Can anyone help me with a solution?
May 16 in Python by ana1504.k
• 7,890 points
99 views

1 answer to this question.

0 votes
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.
answered May 16 by SDeb
• 13,190 points

Related Questions In Python

0 votes
1 answer

Install pip in Python 3.4

Don't worry! Python already comes in default ...READ MORE

answered May 1, 2018 in Python by aayushi
• 750 points
90 views
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 4, 2018 in Python by Omkar
• 67,660 points

edited Aug 8, 2018 by Omkar 1,189 views
0 votes
1 answer

How to use string.replace() in python 3.x

replace() is a method of <class 'str'> ...READ MORE

answered Aug 3, 2018 in Python by Priyaj
• 56,940 points
97 views
0 votes
1 answer

can not decompress lz4 bytes in python

We need an Minimal, Complete, and Verifiable example to ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 56,940 points
137 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 56,940 points
427 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 14, 2018 in Python by Priyaj
• 56,940 points
41 views
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 56,940 points
65 views
+1 vote
1 answer

How is raw_input() and input() in python3.x?

raw_input() was renamed to input() so now input() returns the exact string ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 56,940 points
55 views
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,190 points
38 views
0 votes
1 answer

Indentation of Python in Notepad++

To indent the block, select the entire ...READ MORE

answered Sep 18, 2018 in Python by SDeb
• 13,190 points
316 views