Error copying a file

Peter Hansen peter at engcorp.com
Fri Apr 16 15:22:51 EDT 2004


Stephen Boulet wrote:

> Krzysztof Stachlewski wrote:
>> Stephen Boulet wrote:
>>>  >>> print myfile
>>> E:\Fritz Reiner\Rimsky-Korsakov--Scheherazade.Debussy--La Mer\01 
>>> Symphonic Suite after "A Thousand and One Nights" - The Sea and 
>>> Sinbad's Ship.ogg
>>
>> It seems you are on Windows box.
>> What filesystem do you use?
>> I have just tried to create such a file, but the filesystem (NTFS)
>> refuses to use " as part of the name.
> 
> I'm on win2000. The file is on a CD I burned, and I wanted to copy it to 
>  a file name that doesn't have any quotation marks in it. The problem is 
> that I can't reference the file to begin with. The command:
> 
>    shutil.copy2(myfile,r'D:\foo.ogg')

Krzysztof's idea was excellent, because the quotation marks *are* the
source of the problem.  I don't know why, and maybe it should be
considered a bug on Windows (note I don't say *in* Windows or *in*
Python, because it could be either), but I can get the same behaviour
by creating a file manually on Linux and then trying to access it
through a file share from Windows.  In the following, drive G: is
my Samba-shared drive:

G:\>python
 >>> import os
 >>> os.path.isfile('This is a "test" file')
True
 >>> os.path.isfile('This is a "test" filex')   # just testing
False
 >>> import shutil
 >>> shutil.copy('This is a "test" file', r'c:\test.txt')
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "c:\a\python23\lib\shutil.py", line 71, in copy
     copyfile(src, dst)
   File "c:\a\python23\lib\shutil.py", line 37, in copyfile
     fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: 'This is a "test" file'


Perhaps your only option for now, since it seems shutil.copy
uses the underlying OS copy and that barfs on Windows, is to
open the file and copy it the hard way:

 >>> data = file('This is a "test" file').read()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'This is a "test" file'


Ouch!  That doesn't work either. ;-)

Okay, any reason not to call this a bug in the Windows version of
Python, when os.path.isfile can handle the name but Python can't
open or copy the file?


Note that os.listdir() on my machine shows the 8.3 format name
even though apparently on Stephen's CD it does not:

 >>> os.listdir('.')
['peter', 'im', 'THISI~LT', 'quicken.old']

(Stephen, I think therein lies your solution for now though, which
is to find the 8.3 format name with, say, "DIR /x" or maybe
win32api.GetShortPathName (if that even works) and copy it that
way.)

-Peter



More information about the Python-list mailing list