Windows, subprocess.Popen & encodage

Discussion in 'Python' started by Méta-MCI, May 8, 2007.

  1. Méta-MCI

    Méta-MCI Guest

    Hi!

    From long time, I have problems with strings return, in Windows, by
    subprocess.Popen / stdout.read()

    Last night, I found, by hazard, than if the second byte equal 0, it's,
    perhaps, the solution.
    With a code like this:

    p=subprocess.Popen(u850("cmd /u/c ....
    tdata=p.stdout.read()
    if ord(tdata[1])==0:
    data=tdata.decode('utf-16')
    else:
    data=tdata.decode('cp850')

    Diffrents scripts seem run OK. I had try with:
    - common dir
    - dir on unicode-named-files
    - ping
    - various commands


    But, I don't found anything, in any documentations, on this.



    Sombody can confirm? Am I misled? Am I right?



    * sorry for my bad english*


    @-salutations

    Michel Claveau
    Méta-MCI, May 8, 2007
    #1
    1. Advertising

  2. > But, I don't found anything, in any documentations, on this.
    >
    > Sombody can confirm? Am I misled? Am I right?


    You are right, and you are misled. The encoding of the data
    that you get from Popen.read is not under the control of Python:
    i.e. not only you don't know, but Python doesn't know, either.
    The operating system simply has no mechanism of indicating
    what encoding is used on a pipe.

    So different processes may chose different encodings. Some
    may produce UTF-16, others may produce CP-850, yet others
    UTF-8, and so on. There really is no way to tell other than
    reading the documentation *of the program you run*, and,
    failing that, reading the source code of the program you
    run.

    On Windows, many programs will indeed use one of the
    two system code pages, or UTF-16. It's true that
    UTF-16 can be quite reliably detected by looking at the
    first two bytes. However, the two system code pages
    (OEM CP and ANSI CP) are not so easy to tell apart.

    Regards,
    Martin
    =?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=, May 8, 2007
    #2
    1. Advertising

  3. Méta-MCI

    MC Guest

    Thank you.






    --
    @-salutations

    Michel Claveau
    MC, May 8, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michel Claveau - abstraction méta-galactique non t

    Pb encodage & import

    Michel Claveau - abstraction méta-galactique non t, Jan 6, 2005, in forum: Python
    Replies:
    1
    Views:
    259
    Michel Claveau - abstraction méta-galactique non t
    Jan 6, 2005
  2. madpython

    subprocess.Popen on Windows

    madpython, Jun 23, 2006, in forum: Python
    Replies:
    3
    Views:
    8,225
    Uri Nix
    Jun 25, 2006
  3. Tim Golden
    Replies:
    0
    Views:
    457
    Tim Golden
    Oct 7, 2007
  4. Replies:
    8
    Views:
    743
  5. Werner F. Bruhin

    subprocess.Popen on Windows

    Werner F. Bruhin, Oct 20, 2008, in forum: Python
    Replies:
    1
    Views:
    317
    Eric Carlson
    Oct 20, 2008
Loading...

Share This Page