Error copying a file

Discussion in 'Python' started by Stephen Boulet, Apr 14, 2004.

  1. I know that the name of this file is somewhat pathological, but this if
    weird:

    >>> 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

    >>> os.path.isfile(myfile)

    True
    >>> os.path.getsize(myfile)

    12321468L
    >>> shutil.copy2(myfile,r'D:\foo.ogg')

    Traceback (most recent call last):
    File "<input>", line 1, in ?
    File "C:\Python23\Lib\shutil.py", line 82, in copy2
    copyfile(src, dst)
    File "C:\Python23\Lib\shutil.py", line 37, in copyfile
    fsrc = open(src, 'rb')
    IOError: [Errno 2] No such file or directory: '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'
    >>> shutil.copy(myfile,r'D:\foo.ogg')

    Traceback (most recent call last):
    File "<input>", line 1, in ?
    File "C:\Python23\Lib\shutil.py", line 71, in copy
    copyfile(src, dst)
    File "C:\Python23\Lib\shutil.py", line 37, in copyfile
    fsrc = open(src, 'rb')
    IOError: [Errno 2] No such file or directory: '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'

    Must have CD, btw, for you Fritz Reiner fans ... and yes I do own the cd ;)

    Stephen
     
    Stephen Boulet, Apr 14, 2004
    #1
    1. Advertising

  2. Stephen Boulet wrote:
    > I know that the name of this file is somewhat pathological, but this if
    > weird:
    >
    > >>> 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.

    --
    Stach Tlen: stachobywatelpl, GG: 1811474
    Jabber: stach at jabber atman pl
     
    Krzysztof Stachlewski, Apr 15, 2004
    #2
    1. Advertising

  3. Krzysztof Stachlewski wrote:
    > Stephen Boulet wrote:
    >
    >> I know that the name of this file is somewhat pathological, but this
    >> if weird:
    >>
    >> >>> 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')

    can't use the string held in myfile, although os.listdir('directory on
    CD') contains it as the first entry.

    It would be nice if I could referene the file in a way that would not
    cause the copy command to fail.

    Stephen
     
    Stephen Boulet, Apr 16, 2004
    #3
  4. Stephen Boulet

    Peter Hansen Guest

    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
     
    Peter Hansen, Apr 16, 2004
    #4
  5. Stephen Boulet wrote:

    > 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')
    >
    > can't use the string held in myfile, although os.listdir('directory on
    > CD') contains it as the first entry.
    >
    > It would be nice if I could referene the file in a way that would not
    > cause the copy command to fail.


    Its not a problem with Python, but with the CD-burning software
    which allowed for the illegal char in the filename.
    Can you open the file using Explorer? Or move it to disk and rename?
    That's the best thing you can do about it.

    --
    Stach Tlen: stachobywatelpl, GG: 1811474
    Jabber: stach at jabber atman pl
     
    Krzysztof Stachlewski, Apr 16, 2004
    #5
  6. Peter Hansen wrote:

    > 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?


    Good question.

    > (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


    I can list the file at the DOS prompt:

    "dir /x" gives me "01SYMP~2.OGG" as the short file name, "dir
    01SYMP~2.OGG" does echo back the file name, but "copy 01SYMP~2.OGG"
    gives me a "The system cannot find the file specified" error.

    Oh well, I'll just redo it with a different file name.

    I did burn the CD under linux, with k3b as a front end to cdrecord,
    using joliet extensions with 128 character file names enabled.

    Stephen
     
    Stephen Boulet, Apr 16, 2004
    #6
  7. Peter Hansen wrote:

    > 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?


    Hmmm... isfile() apparently uses some OS functions that don't
    check for valid characters in filenames.
    If it is a bug then it is a bug within Windows.
    But from the Windows point of view, a " in filename
    is simply some piece of corrupted data so it can do anything with it.
    It's good it doesn't display BSOD. ;-)

    --
    Stach Tlen: stachobywatelpl, GG: 1811474
    Jabber: stach at jabber atman pl
     
    Krzysztof Stachlewski, Apr 16, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Waz...
    Replies:
    1
    Views:
    712
    Xander Q.
    Jul 7, 2003
  2. Replies:
    0
    Views:
    504
  3. Replies:
    2
    Views:
    439
    Larry Bates
    Jan 11, 2006
  4. Jared Wiltshire
    Replies:
    8
    Views:
    424
    Ian Collins
    Aug 7, 2006
  5. aki
    Replies:
    1
    Views:
    331
    Keith Thompson
    Oct 26, 2010
Loading...

Share This Page