shutil.copy Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Nicolson

    #1

    shutil.copy Problem

    Hi,

    I wasn't exactly sure where to send this, I don't know if it is a bug
    in Python or not. This is rare, but it has occurred a few times and
    seems to be reproducible for those who experience it.

    Examine this code:
    >>try:
    >> shutil.copy("/file.xml","/Volumes/External/file.xml")
    >>except Exception, err:
    >> print sys.exc_info()[0]
    >> print err
    This is the output:
    exceptions.Unic odeDecodeError
    'ascii' codec can't decode byte 0xd6 in position 26: ordinal not in
    range(128)]

    What could the possible cause of this be? Shouldn't shutil simply be
    reading and writing the bytes and not character decoding them?

    Cheers,
    David


  • Justin Ezequiel

    #2
    Re: shutil.copy Problem

    On Mar 27, 11:10 am, David Nicolson <davidnicols... @hotmail.com>
    wrote:
    Hi,
    >
    I wasn't exactly sure where to send this, I don't know if it is a bug
    in Python or not. This is rare, but it has occurred a few times and
    seems to be reproducible for those who experience it.
    >
    Examine this code:
    >>try:
    >> shutil.copy("/file.xml","/Volumes/External/file.xml")
    >>except Exception, err:
    >> print sys.exc_info()[0]
    >> print err
    >
    This is the output:
    exceptions.Unic odeDecodeError
    'ascii' codec can't decode byte 0xd6 in position 26: ordinal not in
    range(128)]
    >
    What could the possible cause of this be? Shouldn't shutil simply be
    reading and writing the bytes and not character decoding them?
    >
    Cheers,
    David
    what if you try it without the try...except?
    perhaps the UnicodeDecodeEr ror is in the print...

    Comment

    • David Nicolson

      #3
      Re: shutil.copy Problem

      Thanks, but it's definitely not the print. In original the code the
      print statements are replaced by a call to a log method.

      Besides, the exception would be different if it was thrown outside of
      the try block.


      On 27/03/2007, at 2:42 PM, Justin Ezequiel wrote:
      On Mar 27, 11:10 am, David Nicolson <davidnicols... @hotmail.com>
      wrote:
      >Hi,
      >>
      >I wasn't exactly sure where to send this, I don't know if it is a bug
      >in Python or not. This is rare, but it has occurred a few times and
      >seems to be reproducible for those who experience it.
      >>
      >Examine this code:
      >>>>try:
      >>>> shutil.copy("/file.xml","/Volumes/External/file.xml")
      >>>>except Exception, err:
      >>>> print sys.exc_info()[0]
      >>>> print err
      >>
      >This is the output:
      >exceptions.Uni codeDecodeError
      >'ascii' codec can't decode byte 0xd6 in position 26: ordinal not in
      >range(128)]
      >>
      >What could the possible cause of this be? Shouldn't shutil simply be
      >reading and writing the bytes and not character decoding them?
      >>
      >Cheers,
      >David
      >
      what if you try it without the try...except?
      perhaps the UnicodeDecodeEr ror is in the print...
      >
      --
      http://mail.python.org/mailman/listinfo/python-list


      Comment

      • Leo Kislov

        #4
        Re: shutil.copy Problem

        On Mar 26, 8:10 pm, David Nicolson <davidnicols... @hotmail.comwro te:
        Hi,
        >
        I wasn't exactly sure where to send this, I don't know if it is a bug
        in Python or not. This is rare, but it has occurred a few times and
        seems to be reproducible for those who experience it.
        >
        Examine this code:
        >>try:
        >> shutil.copy("/file.xml","/Volumes/External/file.xml")
        >>except Exception, err:
        >> print sys.exc_info()[0]
        >> print err
        >
        This is the output:
        exceptions.Unic odeDecodeError
        'ascii' codec can't decode byte 0xd6 in position 26: ordinal not in
        range(128)]
        >
        What could the possible cause of this be?
        Show us traceback, without it I doubt anyone can help.
        Shouldn't shutil simply be
        reading and writing the bytes and not character decoding them?
        Yes, shutil.copy copies content verbatim.

        -- Leo

        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: shutil.copy Problem

          In <mailman.5648.1 174965082.32031 .python-list@python.org >, David Nicolson
          wrote:
          Hi,
          >
          I wasn't exactly sure where to send this, I don't know if it is a bug
          in Python or not. This is rare, but it has occurred a few times and
          seems to be reproducible for those who experience it.
          >
          Examine this code:
          >>try:
          >> shutil.copy("/file.xml","/Volumes/External/file.xml")
          >>except Exception, err:
          >> print sys.exc_info()[0]
          >> print err
          >
          This is the output:
          exceptions.Unic odeDecodeError
          'ascii' codec can't decode byte 0xd6 in position 26: ordinal not in
          range(128)]
          >
          What could the possible cause of this be? Shouldn't shutil simply be
          reading and writing the bytes and not character decoding them?
          Do you really get this error from the code above or just in the real
          program? Are the paths hard coded in the real program too?

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • Facundo Batista

            #6
            Re: shutil.copy Problem

            David Nicolson wrote:
            Thanks, but it's definitely not the print. In original the code the
            print statements are replaced by a call to a log method.
            >
            Besides, the exception would be different if it was thrown outside of
            the try block.
            The best you can do is take the piece of code that has the problem, show
            it to us, and then copy the traceback.

            Regards,

            --
            .. Facundo
            ..
            Blog: http://www.taniquetil.com.ar/plog/
            PyAr: http://www.python.org/ar/


            Comment

            • John Nagle

              #7
              Re: shutil.copy Problem

              Facundo Batista wrote:
              David Nicolson wrote:
              >
              >
              >>Thanks, but it's definitely not the print. In original the code the
              >>print statements are replaced by a call to a log method.
              >>
              >>Besides, the exception would be different if it was thrown outside of
              >>the try block.
              >
              >
              The best you can do is take the piece of code that has the problem, show
              it to us, and then copy the traceback.
              >
              Regards,
              There may be some problem here with a file being recognized as Unicode
              in binary mode. That shouldn't happen, but looking at the Windows
              UNICODE support, it might not be impossible.

              Information needed:

              - Platform (Windows, Linux, ...)
              - Python version
              - A hex dump of the first few bytes of the input file.

              John Nagle

              Comment

              • David Nicolson

                #8
                Re: shutil.copy Problem

                Hi John,

                That was an excellent idea and it was the cause problem. Whether this
                is a bug in shutil I'm not sure.

                Here is the traceback, Python 2.4.3 on Windows XP:
                C:\Documents and Settings\Güstav >C:\python243\p ython Z:\sh.py
                Copying u'C:\\Documents and Settings\\G\xfc stav\\My Documents\\My
                Music\\iTunes
                \\iTunes Music Library.xml' ...
                Traceback (most recent call last):
                File "Z:\sh.py", line 12, in ?
                shutil.copy(xml file,"C:iTunes Music Library.xml")
                File "C:\python243\l ib\shutil.py", line 81, in copy
                copyfile(src, dst)
                File "C:\python243\l ib\shutil.py", line 41, in copyfile
                if _samefile(src, dst):
                File "C:\python243\l ib\shutil.py", line 36, in _samefile
                return (os.path.normca se(os.path.absp ath(src)) ==
                UnicodeDecodeEr ror: 'ascii' codec can't decode byte 0xfc in
                position 27: ordinal
                not in range(128)
                Here is the code:
                import _winreg
                import os
                import shutil
                >
                reg = _winreg.Connect Registry(None, _winreg.HKEY_CU RRENT_USER)
                key = _winreg.OpenKey (reg, r"Software\Micr osoft\Windows
                \CurrentVersion \Explorer\Shell Folders", 0, _winreg.KEY_REA D)
                mymusic = _winreg.QueryVa lueEx(key, "My Music")[0]
                >
                xmlfile = os.path.join(os .path.join(mymu sic,"iTunes")," iTunes Music
                Library.xml")
                print "Copying ",repr(xmlfile) ,"..."
                >
                shutil.copy(xml file,"C:\iTunes Music Library.xml")
                The shutil line needed to be changed to this to be successful:
                shutil.copy(xml file.encode("wi ndows-1252"),"C:\iTun es Music
                Library.xml"

                Regards,
                David


                On 27/03/2007, at 4:47 PM, John Nagle wrote:
                Facundo Batista wrote:
                >David Nicolson wrote:
                >>
                >>
                >>Thanks, but it's definitely not the print. In original the code the
                >>print statements are replaced by a call to a log method.
                >>>
                >>Besides, the exception would be different if it was thrown
                >>outside of
                >>the try block.
                >>
                >>
                >The best you can do is take the piece of code that has the
                >problem, show
                >it to us, and then copy the traceback.
                >>
                >Regards,
                >
                There may be some problem here with a file being recognized as
                Unicode
                in binary mode. That shouldn't happen, but looking at the Windows
                UNICODE support, it might not be impossible.
                >
                Information needed:
                >
                - Platform (Windows, Linux, ...)
                - Python version
                - A hex dump of the first few bytes of the input file.
                >
                John Nagle
                --
                http://mail.python.org/mailman/listinfo/python-list


                Comment

                • Leo Kislov

                  #9
                  Re: shutil.copy Problem

                  On Mar 28, 7:01 am, David Nicolson <davidnicols... @hotmail.comwro te:
                  Hi John,
                  >
                  That was an excellent idea and it was the cause problem. Whether this
                  is a bug inshutilI'm not sure.
                  >
                  Here is the traceback, Python 2.4.3 on Windows XP:
                  >
                  >
                  >
                  >
                  >
                  C:\Documents and Settings\Güstav >C:\python243\p ython Z:\sh.py
                  Copying u'C:\\Documents and Settings\\G\xfc stav\\My Documents\\My
                  Music\\iTunes
                  \\iTunes Music Library.xml' ...
                  Traceback (most recent call last):
                  File "Z:\sh.py", line 12, in ?
                  shutil.copy(xml file,"C:iTunes Music Library.xml")
                  Note, there is no backslash after C:. shutil will try to make an
                  absolute file name and concatenate it with a current directory name (C:
                  \Documents and Settings\Güstav ) that contains non-ascii characters.
                  Because of backward compatibility the absolute name won't be unicode.
                  On the other hand data coming from registry is unicode. When shutil
                  tries to compare those two file names it fails. To avoid the problem
                  you need either make both file names unicode or both file names byte-
                  strings.

                  However one thing is still mystery to me. Your source code contains
                  backslash but your traceback doesn't:
                  shutil.copy(xml file,"C:\iTunes Music Library.xml")
                  >

                  Theshutilline needed to be changed to this to be successful:
                  >
                  shutil.copy(xml file.encode("wi ndows-1252"),"C:\iTun es Music
                  Library.xml"
                  It will work only in some European locales. Using of locale module you
                  can make it work for 99% of world users, but it will still fail in
                  cases like German locale and Greek characters in file names. Only
                  using unicode everywhere in your program is a complete solution. Like

                  shutil.copy(xml file, u"C:\iTunes Music Library.xml")

                  if you use constant or make sure your file name is unicode:

                  dest = unicode(....)
                  shutil.copy(xml file, dest)


                  -- Leo.

                  Comment

                  Working...