473,887 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Mar 27 '07 #1
8 7641
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...

Mar 27 '07 #2
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


Mar 27 '07 #3
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

Mar 27 '07 #4
In <ma************ *************** ************@py thon.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
Mar 27 '07 #5
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/
Mar 27 '07 #6
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
Mar 27 '07 #7
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


Mar 28 '07 #8
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.

Apr 1 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7787
by: bmgz | last post by:
I am have made a simple script that moves all desktop clutter (ie files that are not *.lnk) to a specified folder eg. c:\myhome\mydocs\desktopdebris\2003-12-16 ---------------------------------------------------------------------------- ----------------- import re, os, time, shutil os.chdir(os.environ+os.environ+"\\Desktop") DESKTOP = os.listdir(os.environ+os.environ+"\\Desktop")
2
2114
by: BartlebyScrivener | last post by:
I'm working on various backup scripts, using filecmp and shutil. When I run a script to copy files to a mapped network drive, shutil creates a backup file with a date of 2002 or so. If I use shutil.copy2 it copies the dates of the original files, but creates folders with similary old dates and times. Anybody ever seen that before? My system clock is fine. I don't know what else to try.
1
3470
by: Russell Warren | last post by:
I just did a comparison of the copying speed of shutil.copy against the speed of a direct windows copy using os.system. I copied a file that was 1083 KB. I'm very interested to see that the shutil.copy copyfileobj implementation of hacking through the file and writing a new one is significantly faster... any clue as to why this is? I figure I'm missing something here. Does os.system launch a cmd shell every time?
6
14593
by: Antoine De Groote | last post by:
Google tells quite some things about it, but none of them are satisfactory. I'm on Windows, and shutil operations (e.g. move, copy) throw Permission denied all the time, for the source files. It seems that this is the case for all my files. But what I don't understand is that yesterday it still worked. I didn't change anything on my system though (at least not that I am aware of). I restarted the computer several times to see if that...
1
6241
by: Hugo Ferreira | last post by:
Hi there, I have a problem. I'm using calling shutil.copyfile() followed by open(). The thing is that most of the times open() is called before the actual file is copied. I don't have this problem when doing a step-by-step debug, since I give enough time for the OS to copy the file, but at run-time, it throws an exception. Is there anyway to force a sync copy of the file (make python wait for the completion)?
10
3337
by: Robert Dailey | last post by:
Hi, I'm trying to create a Python equivalent of the C++ "ifstream" class, with slight behavior changes. Basically, I want to have a "filestream" object that will allow you to overload the '<<' and '>>' operators to stream out and stream in data, respectively. So far this is what I have: class filestream:
10
3238
by: yinglcs | last post by:
Hi, Is there a c library which does shutil.copy2() in python? Basically copy a file from 1 directory to another? import shutil import os shutil.copy2(r"C:\test\test",r"C:\test1\test")
1
2765
by: Ahmed, Shakir | last post by:
Need help to copy a personal geodatabase from one location to another: Trying to copy a personal geodatabase from one location to another location where all the users are retrieving data from the second location: 1. I can copy over the updated personal geodatabase to the working location and overwrite it, though the file is opened by ArcGIS users (Local Installation of Arc GIS on the users computers).
4
2609
by: klia | last post by:
hello folks i am trying to tweak the current codes so that later when i call it from the terminal i can provide sourcefile and the destination file rather being fixed in the code. because now i have to specify the sourcefile and the destinationfile in codes and not left to be specified from the terminal. i want to be able to do this. python shutil_copy.py sourcefile, destinationfile import shutil shutil.copyfile(srcfile, dstfile)...
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10770
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10875
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10432
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.