problem with special characters in filename

Discussion in 'Python' started by fizzotti@gmail.com, Jan 23, 2009.

  1. Guest

    using:

    Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
    [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2


    Hello

    I have two files (let's assume they are all in the same directory):

    1) "a.dat" containing the single line "Sébastien.dat"
    2) "Sébastien.dat" containing arbitraty data

    I want to:

    open "a.dat"
    read in the single line ("Sébastien.dat")
    copy the file described by the single line ("Sébastien.dat") to
    another location with name "b.dat"

    This is what I have started with:

    import shutil

    file = open('a.dat','r') #opens file for reading
    for line in file:
    print line
    old_name = line[0:len(line)-1] #gets rid of "\n" suffix
    print old_name
    shutil.copy(old_name, 'b.dat') #copies file

    However I get the following error:
    IOError: [Errno 2] No such file or directory: 'S\xc3\xa9bastien.dat'

    I'm sure this has something to do with encoding and decoding UTF-8 and
    Unicode or something like that, but everything I've tried has yet to
    produce any favourable results.

    Thanks
    Dino
    , Jan 23, 2009
    #1
    1. Advertising

  2. schrieb:
    > using:
    >
    > Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
    > [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
    >
    >
    > Hello
    >
    > I have two files (let's assume they are all in the same directory):
    >
    > 1) "a.dat" containing the single line "Sébastien.dat"
    > 2) "Sébastien.dat" containing arbitraty data
    >
    > I want to:
    >
    > open "a.dat"
    > read in the single line ("Sébastien.dat")
    > copy the file described by the single line ("Sébastien.dat") to
    > another location with name "b.dat"
    >
    > This is what I have started with:
    >
    > import shutil
    >
    > file = open('a.dat','r') #opens file for reading
    > for line in file:
    > print line
    > old_name = line[0:len(line)-1] #gets rid of "\n" suffix
    > print old_name
    > shutil.copy(old_name, 'b.dat') #copies file
    >
    > However I get the following error:
    > IOError: [Errno 2] No such file or directory: 'S\xc3\xa9bastien.dat'
    >
    > I'm sure this has something to do with encoding and decoding UTF-8 and
    > Unicode or something like that, but everything I've tried has yet to
    > produce any favourable results.


    You need to know two things:

    - what encoding has the a.dat-content?

    - what encoding uses your filesystem?

    Then you need to decode the file's contents with the first one, and then
    encode it with the second one.

    The above at least applies to Linux. I'm not sure about windows - I
    *know* they have wide-char/unicode support, but I'm not sure how exactly
    that is exposed via the python file apis

    Diez
    Diez B. Roggisch, Jan 23, 2009
    #2
    1. Advertising

  3. >
    > The above at least applies to Linux. I'm not sure about windows - I
    > *know* they have wide-char/unicode support, but I'm not sure how exactly
    > that is exposed via the python file apis


    Just found this:

    http://www.amk.ca/python/howto/unicode


    there is a seciton about filenames and OSes in there, you should read it.

    Diez
    Diez B. Roggisch, Jan 23, 2009
    #3
    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. Andreas
    Replies:
    0
    Views:
    716
    Andreas
    Mar 2, 2004
  2. Stefan Mueller
    Replies:
    3
    Views:
    32,952
    Stefan Mueller
    Jul 23, 2006
  3. Replies:
    2
    Views:
    1,070
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,635
    rvino
    Aug 14, 2007
  5. majna
    Replies:
    4
    Views:
    638
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
Loading...

Share This Page