shutil.copy Problem
David Nicolson
davidnicolson1 at hotmail.com
Wed Mar 28 10:01:05 EDT 2007
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\python Z:\sh.py
> Copying u'C:\\Documents and Settings\\G\xfcstav\\My Documents\\My
> Music\\iTunes
> \\iTunes Music Library.xml' ...
> Traceback (most recent call last):
> File "Z:\sh.py", line 12, in ?
> shutil.copy(xmlfile,"C:iTunes Music Library.xml")
> File "C:\python243\lib\shutil.py", line 81, in copy
> copyfile(src, dst)
> File "C:\python243\lib\shutil.py", line 41, in copyfile
> if _samefile(src, dst):
> File "C:\python243\lib\shutil.py", line 36, in _samefile
> return (os.path.normcase(os.path.abspath(src)) ==
> UnicodeDecodeError: '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.ConnectRegistry(None, _winreg.HKEY_CURRENT_USER)
> key = _winreg.OpenKey(reg, r"Software\Microsoft\Windows
> \CurrentVersion\Explorer\Shell Folders", 0, _winreg.KEY_READ)
> mymusic = _winreg.QueryValueEx(key, "My Music")[0]
>
> xmlfile = os.path.join(os.path.join(mymusic,"iTunes"),"iTunes Music
> Library.xml")
> print "Copying ",repr(xmlfile),"..."
>
> shutil.copy(xmlfile,"C:\iTunes Music Library.xml")
The shutil line needed to be changed to this to be successful:
> shutil.copy(xmlfile.encode("windows-1252"),"C:\iTunes 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
More information about the Python-list
mailing list