1

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')
        -------------------------------------------------------------------------------
    
    Oscarzhao
    • 75
    • 1
    • 9

    2 Answers2

    1

    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()
    
    BlackJack
    • 4,476
    • 1
    • 20
    • 25
    0

    Your encoding magic comment isn't correct. change it to #-*- coding=utf-8 -*-.

    See pep 0263 for more info.

    clarkep
    • 795
    • 1
    • 6
    • 12
    • '# encoding=utf-8', '#coding=utf-8', '#-*- coding=utf-8 -*-' all work correctly, More precisely, the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)". " --from the page pep0263 – Oscarzhao Jul 11 '14 at 03:23