question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

can not handle the output from python build-in lib(while using argparse)

See original GitHub issue

I have below code :

from __future__ import unicode_literals, absolute_import

......

def main():
    global ROOT_PATH
    parser = argparse.ArgumentParser()
    parser.add_argument('-p','--path', default='__here__')
    print 1
    args = parser.parse_args()
    print 2
    p = unicode(args.path, 'gbk')
    if p == '__here__':
        ROOT_PATH = os.getcwd()
    else:
        ROOT_PATH = p

    for sf in get_sub_folders(ROOT_PATH):
        process_folder(sf)

My windows7 cmd encoding is gbk(a Chinese encoding)

Chinese work fine

E:\[Sync]\project\auto_shift>python exe.py -p E:\[Sync]\【垃圾箱】
1
2

But Japanese go wrong.

E:\[Sync]\project\auto_shift>python exe.py -p E:\同人本\シュート・ザ・ムーン (フエタキシ)
1
usage: exe.py [-h] [-p PATH]
Traceback (most recent call last):
  File "exe.py", line 196, in <module>
    main()
  File "exe.py", line 183, in main
    args = parser.parse_args()
  File "D:\Python27\lib\argparse.py", line 1704, in parse_args
    self.error(msg % ' '.join(argv))
  File "D:\Python27\lib\argparse.py", line 2374, in error
    self.exit(2, _('%s: error: %s\n') % (self.prog, message))
  File "D:\Python27\lib\argparse.py", line 2361, in exit
    self._print_message(message, _sys.stderr)
  File "D:\Python27\lib\argparse.py", line 2354, in _print_message
    file.write(message)
  File "D:\Python27\lib\site-packages\win_unicode_console-0.4-py2.7.egg\win_unicode_console\streams.py", line 217, in wr
ite
    s = s.decode(self.encoding)
  File "D:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 40: invalid start byte

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Drekincommented, Jun 4, 2016

Generally, there may problem with giving Unicode argument to a Python 2 script, the bytes in sys.argv may even not be a faithful representation of the original string. See #20 . Maybe I’ll add code like that to win_unicode_console.

0reactions
Drekincommented, Jun 13, 2016

There is some progress with #20. But for your particular code, you may turn sys.argv to Unicode before parsing rather than after:

args = [unicode(arg, "gbk") if not isinstance(arg, unicode) else arg for arg in sys.argv]
args = parser.parse_args(args)
Read more comments on GitHub >

github_iconTop Results From Across the Web

argparse — Parser for command-line options, arguments and ...
default. Default value used when an argument is not provided. Defaults to None. dest. Specify the attribute name used in the result namespace....
Read more >
argparse – Command line option and argument parsing.
The first step when using argparse is to create a parser object and tell it what arguments to expect. The parser can then...
Read more >
Build Command-Line Interfaces With Python's argparse
In Python, you can create full-featured CLIs with the argparse module from the standard library. In this article, you'll learn how to:.
Read more >
How to open file using argparse?
Running this i have error: parser. add_argument('file', type = file) NameError: name 'file' is not defined I don't exaclty understand how argparse work......
Read more >
How To Use argparse to Write Command-Line Programs in ...
Python's argparse standard library module is a tool that helps you write command-line interfaces (CLI) over your Python code. You may already be ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found