Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 199233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:00:21+00:00 2026-05-11T17:00:21+00:00

I want my Python script to be able to read Unicode command line arguments

  • 0

I want my Python script to be able to read Unicode command line arguments in Windows. But it appears that sys.argv is a string encoded in some local encoding, rather than Unicode. How can I read the command line in full Unicode?

Example code: argv.py

import sys

first_arg = sys.argv[1]
print first_arg
print type(first_arg)
print first_arg.encode("hex")
print open(first_arg)

On my PC set up for Japanese code page, I get:

C:\temp>argv.py "PC・ソフト申請書08.09.24.doc"
PC・ソフト申請書08.09.24.doc
<type 'str'>
50438145835c83748367905c90bf8f9130382e30392e32342e646f63
<open file 'PC・ソフト申請書08.09.24.doc', mode 'r' at 0x00917D90>

That’s Shift-JIS encoded I believe, and it “works” for that filename. But it breaks for filenames with characters that aren’t in the Shift-JIS character set—the final “open” call fails:

C:\temp>argv.py Jörgen.txt
Jorgen.txt
<type 'str'>
4a6f7267656e2e747874
Traceback (most recent call last):
  File "C:\temp\argv.py", line 7,
in <module>
    print open(first_arg)
IOError: [Errno 2] No such file or directory: 'Jorgen.txt'

Note—I’m talking about Python 2.x, not Python 3.0. I’ve found that Python 3.0 gives sys.argv as proper Unicode. But it’s a bit early yet to transition to Python 3.0 (due to lack of 3rd party library support).

Update:

A few answers have said I should decode according to whatever the sys.argv is encoded in. The problem with that is that it’s not full Unicode, so some characters are not representable.

Here’s the use case that gives me grief: I have enabled drag-and-drop of files onto .py files in Windows Explorer. I have file names with all sorts of characters, including some not in the system default code page. My Python script doesn’t get the right Unicode filenames passed to it via sys.argv in all cases, when the characters aren’t representable in the current code page encoding.

There is certainly some Windows API to read the command line with full Unicode (and Python 3.0 does it). I assume the Python 2.x interpreter is not using it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:00:21+00:00Added an answer on May 11, 2026 at 5:00 pm

    Here is a solution that is just what I’m looking for, making a call to the Windows GetCommandLineArgvW function:
    Get sys.argv with Unicode characters under Windows (from ActiveState)

    But I’ve made several changes, to simplify its usage and better handle certain uses. Here is what I use:

    win32_unicode_argv.py

    """
    win32_unicode_argv.py
    
    Importing this will replace sys.argv with a full Unicode form.
    Windows only.
    
    From this site, with adaptations:
          http://code.activestate.com/recipes/572200/
    
    Usage: simply import this module into a script. sys.argv is changed to
    be a list of Unicode strings.
    """
    
    
    import sys
    
    def win32_unicode_argv():
        """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
        strings.
    
        Versions 2.x of Python don't support Unicode in sys.argv on
        Windows, with the underlying Windows API instead replacing multi-byte
        characters with '?'.
        """
    
        from ctypes import POINTER, byref, cdll, c_int, windll
        from ctypes.wintypes import LPCWSTR, LPWSTR
    
        GetCommandLineW = cdll.kernel32.GetCommandLineW
        GetCommandLineW.argtypes = []
        GetCommandLineW.restype = LPCWSTR
    
        CommandLineToArgvW = windll.shell32.CommandLineToArgvW
        CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
        CommandLineToArgvW.restype = POINTER(LPWSTR)
    
        cmd = GetCommandLineW()
        argc = c_int(0)
        argv = CommandLineToArgvW(cmd, byref(argc))
        if argc.value > 0:
            # Remove Python executable and commands if present
            start = argc.value - len(sys.argv)
            return [argv[i] for i in
                    xrange(start, argc.value)]
    
    sys.argv = win32_unicode_argv()
    

    Now, the way I use it is simply to do:

    import sys
    import win32_unicode_argv
    

    and from then on, sys.argv is a list of Unicode strings. The Python optparse module seems happy to parse it, which is great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use cURL like the example below. I should… May 11, 2026 at 6:58 pm
  • Editorial Team
    Editorial Team added an answer .NET is expecting a path like ~/folder/file.txt or /folder/file.txt which… May 11, 2026 at 6:58 pm
  • Editorial Team
    Editorial Team added an answer I believe you'll have to do it manually. Apparently it… May 11, 2026 at 6:58 pm

Related Questions

In SCons, my command generators create ridiculously long command lines. I'd like to be
I've been trying to build a simple prototype application in Django, and am reaching
I want to move various parts of my app into simple scripts, to allow
I am looking for a way to extract / scrape data from Word files
I have a third-party product, a terminal emulator, which provides a DLL that can

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.