shutil.copy Problem

Leo Kislov Leo.Kislov at gmail.com
Sun Apr 1 18:02:45 EDT 2007


On Mar 28, 7:01 am, David Nicolson <davidnicols... at hotmail.com> wrote:
> 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\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")

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(xmlfile,"C:\iTunes Music Library.xml")
>



> Theshutilline needed to be changed to this to be successful:
>
> >shutil.copy(xmlfile.encode("windows-1252"),"C:\iTunes 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(xmlfile, u"C:\iTunes Music Library.xml")

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

dest = unicode(....)
shutil.copy(xmlfile, dest)


  -- Leo.




More information about the Python-list mailing list