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, 2019 in Python by ana1504.k
• 7,890 points
265 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, 2019 by SDeb
• 13,250 points

Related Questions In Python

+1 vote
1 answer

How 2 compare words read by sys.stdin.sys() in Python 3?

Hi Alex, You could use something like this: import ...READ MORE

answered Jan 20 in Python by Yamini
206 views
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
285 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
• 69,060 points

edited Aug 8, 2018 by Omkar 3,314 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
• 57,530 points
221 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
• 57,530 points
650 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
• 57,530 points
96 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
• 57,530 points
117 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
• 57,530 points
110 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,250 points
84 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,250 points
654 views