pythonwindowsgbk

python call command line with Chinese Characters in windows fails


Under python in windows, in py script (test.py), os.system or subprocess.Popen calling the dos command fails, but succeeds under command line (windows->cmd.exe), the script is as the following additional info:

  • 1. the dos (windows->cmd.exe) encoding is 'cp936', belonging to 'gbk'
  • 2. I have decoded the test.py into utf-8 by notepad++
  • 3 .decode('gbk'), .encode('gbk'), .decode('utf-8'), .enode('utf-8') have been tried
  • 4. I do not know why
  • Error information:

        C:\Python27\python.exe E:/XtTradeClient/test.py
        File "E:/XtTradeClient/test.py", line 5 
        SyntaxError: Non-ASCII character '\xba' in file E:/XtTradeClient/test.py on line 5,but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    
        Process finished with exit code 1
    
        -------------------------------------------------------------------------------
        # encoding='utf-8'
        import os
    
        if __name__ == "__main__":
            info = '汉字'
            cmd = 'echo ' + info
            if 0 != os.system(cmd):
                raise Exception('failed to call 'echo in command')
        -------------------------------------------------------------------------------
    

    Solution

  • The shown error comes from a missing encoding comment. It looks like you tried to add one, but the format is wrong, so it is not recognized. Try:

    # encoding: utf-8
    

    Then the next problem might be the encoding the shell expects. If your source code is saved as UTF-8 then the (byte) strings in it are UTF-8 encoded and handed that way to os.system(). You may have to recode for the application that actually displays those bytes as characters. That is: first decoding from UTF-8 to unicode and then from unicode to a str in the encoding of the terminal. The explicit decoding can be avoided by using unicode literals, at least for (byte) strings with characters outside the ASCII range.

    # encoding: utf-8
    import os
    
    
    def main():
        info = u'汉字'
        cmd = 'echo ' + info
        if not os.system(cmd.encode('gbk')):
            raise Exception('failed to call `echo` command')
    
    
    if __name__ == '__main__':
        main()