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?
3 days ago in Python by ana1504.k
• 5,870 points
8 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 3 days ago by SDeb
• 9,300 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
40 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
• 65,810 points

edited Aug 8, 2018 by Omkar 266 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,100 points
23 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,100 points
39 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,100 points
118 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,100 points
22 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,100 points
26 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,100 points
25 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
• 9,300 points
20 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
• 9,300 points
95 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.