Chinese character error

Discussion in 'Python' started by John Deas, Feb 8, 2008.

  1. John Deas

    John Deas Guest

    Hi,

    I made a small script to recursively copy files from a directory tree
    to an exportDir only if they have an mp3 extension :

    a=os.walk(os.getcwd())
    for root, dirs, files in a:
    for currFile in files:
    pathCurrFile=os.path.join(root, currFile)
    if mp3Reg.search(pathCurrFile):
    shutil.copy(pathCurrFile,exportDir)
    else:
    print pathCurrFile

    The problem is that I get stuck with files containing name in
    Chinese :

    Traceback (most recent call last):
    File "/cygdrive/c/Documents and Settings/vku/Mes documents/Ma
    musique/iTunes/i
    Tunes Music/script.py", line 21, in <module>
    shutil.copy(pathCurrFile,exportDir)
    File "/usr/lib/python2.5/shutil.py", line 80, in copy
    copyfile(src, dst)
    File "/usr/lib/python2.5/shutil.py", line 46, in copyfile
    fsrc = open(src, 'rb')
    IOError: [Errno 2] No such file or directory: '/cygdrive/c/Documents
    and Setting
    s/vku/Mes documents/Ma musique/iTunes/iTunes Music/Podcasts/Learn
    Chinese - Chin
    esePod/785 Advanced - ????.mp3'

    I am using python on cygwin, so could this be the source of the error,
    and is there a way to fix this ?
     
    John Deas, Feb 8, 2008
    #1
    1. Advertising

  2. John Deas

    Chris Guest

    On Feb 8, 11:29 am, John Deas <> wrote:
    > Hi,
    >
    > I made a small script to recursively copy files from a directory tree
    > to an exportDir only if they have an mp3 extension :
    >
    > a=os.walk(os.getcwd())
    > for root, dirs, files in a:
    > for currFile in files:
    > pathCurrFile=os.path.join(root, currFile)
    > if mp3Reg.search(pathCurrFile):
    > shutil.copy(pathCurrFile,exportDir)
    > else:
    > print pathCurrFile
    >
    > The problem is that I get stuck with files containing name in
    > Chinese :
    >
    > Traceback (most recent call last):
    > File "/cygdrive/c/Documents and Settings/vku/Mes documents/Ma
    > musique/iTunes/i
    > Tunes Music/script.py", line 21, in <module>
    > shutil.copy(pathCurrFile,exportDir)
    > File "/usr/lib/python2.5/shutil.py", line 80, in copy
    > copyfile(src, dst)
    > File "/usr/lib/python2.5/shutil.py", line 46, in copyfile
    > fsrc = open(src, 'rb')
    > IOError: [Errno 2] No such file or directory: '/cygdrive/c/Documents
    > and Setting
    > s/vku/Mes documents/Ma musique/iTunes/iTunes Music/Podcasts/Learn
    > Chinese - Chin
    > esePod/785 Advanced - ????.mp3'
    >
    > I am using python on cygwin, so could this be the source of the error,
    > and is there a way to fix this ?


    It has to do with the way the OS reports the filename. Explorers GUI
    diplays it as square blocks and both CmdPrompt + Cygwin display it as
    Question marks as does the os.listdir in Python. Copying Chinese
    Characters and checking their Ordinal Values directly from python gave
    me for eg. 230+188+162 for 1 Chinese Charater yet Python shows it is
    Ordinal 63 (a Question Mark) after reading the filename.

    Those files you will need to manually copy, I even tried
    find /cygdrive/d/Temp/ -name "*.mp3" -exec cp {} /cygdrive/d/Temp/
    test/ \;
    which yielded
    cp: cannot stat '/cygdrive/d/Temp/??.mp3': No such file or directory
     
    Chris, Feb 8, 2008
    #2
    1. Advertising

  3. John Deas

    Mark Tolonen Guest

    "Chris" <> wrote in message
    news:...
    > On Feb 8, 11:29 am, John Deas <> wrote:
    >> Hi,
    >>
    >> I made a small script to recursively copy files from a directory tree
    >> to an exportDir only if they have an mp3 extension :
    >>
    >> a=os.walk(os.getcwd())
    >> for root, dirs, files in a:
    >> for currFile in files:
    >> pathCurrFile=os.path.join(root, currFile)
    >> if mp3Reg.search(pathCurrFile):
    >> shutil.copy(pathCurrFile,exportDir)
    >> else:
    >> print pathCurrFile
    >>
    >> The problem is that I get stuck with files containing name in
    >> Chinese :
    >>
    >> Traceback (most recent call last):
    >> File "/cygdrive/c/Documents and Settings/vku/Mes documents/Ma
    >> musique/iTunes/i
    >> Tunes Music/script.py", line 21, in <module>
    >> shutil.copy(pathCurrFile,exportDir)
    >> File "/usr/lib/python2.5/shutil.py", line 80, in copy
    >> copyfile(src, dst)
    >> File "/usr/lib/python2.5/shutil.py", line 46, in copyfile
    >> fsrc = open(src, 'rb')
    >> IOError: [Errno 2] No such file or directory: '/cygdrive/c/Documents
    >> and Setting
    >> s/vku/Mes documents/Ma musique/iTunes/iTunes Music/Podcasts/Learn
    >> Chinese - Chin
    >> esePod/785 Advanced - ????.mp3'
    >>
    >> I am using python on cygwin, so could this be the source of the error,
    >> and is there a way to fix this ?

    >
    > It has to do with the way the OS reports the filename. Explorers GUI
    > diplays it as square blocks and both CmdPrompt + Cygwin display it as
    > Question marks as does the os.listdir in Python. Copying Chinese
    > Characters and checking their Ordinal Values directly from python gave
    > me for eg. 230+188+162 for 1 Chinese Charater yet Python shows it is
    > Ordinal 63 (a Question Mark) after reading the filename.
    >
    > Those files you will need to manually copy, I even tried
    > find /cygdrive/d/Temp/ -name "*.mp3" -exec cp {} /cygdrive/d/Temp/
    > test/ \;
    > which yielded
    > cp: cannot stat '/cygdrive/d/Temp/??.mp3': No such file or directory


    If you call os.walk() with a Unicode string, it's return values will be
    Unicode as well and you should be able to process files with non-ASCII
    characters. This worked for me (on Windows):

    import os
    import shutil
    import fnmatch

    exportDir = u'c:\\mp3s'

    a=os.walk(os.getcwdu()) # Unicode version of os.getcwd()
    for root, dirs, files in a:
    for currFile in files:
    pathCurrFile=os.path.join(root, currFile)
    if fnmatch.fnmatch(pathCurrFile,u'*.mp3'):
    shutil.copy(pathCurrFile,exportDir)
    else:
    print pathCurrFile

    --Mark
     
    Mark Tolonen, Feb 9, 2008
    #3
  4. John Deas

    John Deas Guest

    On Feb 9, 3:00 am, "Mark Tolonen" <>
    wrote:
    > "Chris" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > On Feb 8, 11:29 am, John Deas <> wrote:
    > >> Hi,

    >
    > >> I made a small script to recursively copy files from a directory tree
    > >> to an exportDir only if they have an mp3 extension :

    >
    > >> a=os.walk(os.getcwd())
    > >> for root, dirs, files in a:
    > >> for currFile in files:
    > >> pathCurrFile=os.path.join(root, currFile)
    > >> if mp3Reg.search(pathCurrFile):
    > >> shutil.copy(pathCurrFile,exportDir)
    > >> else:
    > >> print pathCurrFile

    >
    > >> The problem is that I get stuck with files containing name in
    > >> Chinese :

    >
    > >> Traceback (most recent call last):
    > >> File "/cygdrive/c/Documents and Settings/vku/Mes documents/Ma
    > >> musique/iTunes/i
    > >> Tunes Music/script.py", line 21, in <module>
    > >> shutil.copy(pathCurrFile,exportDir)
    > >> File "/usr/lib/python2.5/shutil.py", line 80, in copy
    > >> copyfile(src, dst)
    > >> File "/usr/lib/python2.5/shutil.py", line 46, in copyfile
    > >> fsrc = open(src, 'rb')
    > >> IOError: [Errno 2] No such file or directory: '/cygdrive/c/Documents
    > >> and Setting
    > >> s/vku/Mes documents/Ma musique/iTunes/iTunes Music/Podcasts/Learn
    > >> Chinese - Chin
    > >> esePod/785 Advanced - ????.mp3'

    >
    > >> I am using python oncygwin, so could this be the source of the error,
    > >> and is there a way to fix this ?

    >
    > > It has to do with the way the OS reports the filename. Explorers GUI
    > > diplays it as square blocks and both CmdPrompt +Cygwindisplay it as
    > > Question marks as does the os.listdir in Python. Copying Chinese
    > > Characters and checking their Ordinal Values directly from python gave
    > > me for eg. 230+188+162 for 1 Chinese Charater yet Python shows it is
    > > Ordinal 63 (a Question Mark) after reading the filename.

    >
    > > Those files you will need to manually copy, I even tried
    > > find /cygdrive/d/Temp/ -name "*.mp3" -exec cp {} /cygdrive/d/Temp/
    > > test/ \;
    > > which yielded
    > > cp: cannot stat '/cygdrive/d/Temp/??.mp3': No such file or directory

    >
    > If you call os.walk() with a Unicode string, it's return values will be
    > Unicode as well and you should be able to process files with non-ASCII
    > characters. This worked for me (on Windows):
    >
    > import os
    > import shutil
    > import fnmatch
    >
    > exportDir = u'c:\\mp3s'
    >
    > a=os.walk(os.getcwdu()) # Unicode version of os.getcwd()
    > for root, dirs, files in a:
    > for currFile in files:
    > pathCurrFile=os.path.join(root, currFile)
    > if fnmatch.fnmatch(pathCurrFile,u'*.mp3'):
    > shutil.copy(pathCurrFile,exportDir)
    > else:
    > print pathCurrFile
    >
    > --Mark


    Hi,

    I tried to play around with the advice you gave me.

    When I do this in the directory containing an unicode-named mp3:

    import os, shutil
    os.listdir(os.getcwdu())

    I get :

    [u'The Pixies - Where is my mind (fight club).mp3', u'???.mp3']

    Than, if try to copy it with :

    shutil.copy(os.listdir(os.getcwdu())[1],u'test.mp3')

    I get :

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/shutil.py", line 80, in copy
    copyfile(src, dst)
    File "/usr/lib/python2.5/shutil.py", line 46, in copyfi
    fsrc = open(src, 'rb')
    IOError: [Errno 2] No such file or directory: u'???.mp3'

    Have you any idea why this is not working on my computer ?

    Tanks,

    John Deas
     
    John Deas, Feb 10, 2008
    #4
  5. > Have you any idea why this is not working on my computer ?

    Can you please try the listdir operation with the Python distribution
    from python.org instead of Cygwin Python?

    Regards,
    Martin
     
    Martin v. Löwis, Feb 11, 2008
    #5
  6. John Deas

    John Deas Guest

    On Feb 11, 6:51 am, "Martin v. Löwis" <> wrote:
    > > Have you any idea why this is not working on my computer ?

    >
    > Can you please try the listdir operation with the Python distribution
    > from python.org instead ofCygwinPython?
    >
    > Regards,
    > Martin


    it works. Thanks.
     
    John Deas, Feb 13, 2008
    #6
    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. =?Utf-8?B?U3BpZGVyX0ppYQ==?=

    how to diaplay chinese character in aspx page

    =?Utf-8?B?U3BpZGVyX0ppYQ==?=, May 27, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    768
    Natty Gur
    May 28, 2004
  2. Jeff
    Replies:
    3
    Views:
    811
    chris
    Jan 16, 2004
  3. Jeff
    Replies:
    3
    Views:
    804
    Jon A. Cruz
    Jan 17, 2004
  4. chad
    Replies:
    3
    Views:
    689
    Alun Harford
    Mar 5, 2004
  5. KFC
    Replies:
    1
    Views:
    645
Loading...

Share This Page