FAQ
Hello,
I am facing problems while using os.system() of python for
copying a file from source to destination where the file name is in
unicode (multi lingual characters).My python interpreter is trying to
convert them to ascii and is failing. I tried giving unicode string as
input for the function something like:

c = u"copy "+"\""+full_path+"\" "+dest
os.system (c)

where the full_path contains filenames with unicode characters(unicode
data type). I tried searching in google and came to know that UNICODE
support wasn't there for os.system function till python2.3[1].
Currently I am using python2.6 but couldn't find anywhere whether a
support has been provided or not. Can some one suggest me any work
around for this. If I do a manual encoding of the file name to ascii,
it results in loss of data and hence the copy command wouldn't find
the file name.


---------------------------------------------------------------------------------------------------------
Traceback (most recent call last):
File "C:\unitest.py", line 32, in <module>
findFile(u"E:\\New Folder")
File "C:\unitest.py", line 17, in findFile
findFile(full_path)
File "C:\unitest.py", line 17, in findFile
findFile(full_path)
File "C:\unitest.py", line 27, in findFile
os.system (c)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
47-72: ordinal not in range(128)
------------------------------------------------------------------------------------------------------




[1] http://mail.python.org/pipermail/python-list/2003-January/182182.html





Thanks in advance,
Venu M

Search Discussions

  • Venutaurus539 at Feb 25, 2009 at 4:22 am
    Hello,
    I am facing problems while using os.system() of python for
    copying a file from source to destination where the file name is in
    unicode (multi lingual characters).My python interpreter is trying to
    convert them to ascii and is failing. I tried giving unicode string as
    input for the function something like:

    c = u"copy "+"\""+full_path+"\" "+dest
    os.system (c)

    where the full_path contains filenames with unicode characters(unicode
    data type). I tried searching in google and came to know that UNICODE
    support wasn't there for os.system function till python2.3[1].
    Currently I am using python2.6 but couldn't find anywhere whether a
    support has been provided or not. Can some one suggest me any work
    around for this. If I do a manual encoding of the file name to ascii,
    it results in loss of data and hence the copy command wouldn't find
    the file name.


    ---------------------------------------------------------------------------------------------------------
    Traceback (most recent call last):
    File "C:\unitest.py", line 32, in <module>
    findFile(u"E:\\New Folder")
    File "C:\unitest.py", line 17, in findFile
    findFile(full_path)
    File "C:\unitest.py", line 17, in findFile
    findFile(full_path)
    File "C:\unitest.py", line 27, in findFile
    os.system (c)
    UnicodeEncodeError: 'ascii' codec can't encode characters in position
    47-72: ordinal not in range(128)
    ------------------------------------------------------------------------------------------------------




    [1] http://mail.python.org/pipermail/python-list/2003-January/182182.html





    Thanks in advance,
    Venu M
  • Christian Heimes at Feb 25, 2009 at 4:55 am

    venutaurus539 at gmail.com schrieb:
    Hello,
    I am facing problems while using os.system() of python for
    copying a file from source to destination where the file name is in
    unicode (multi lingual characters).My python interpreter is trying to
    convert them to ascii and is failing. I tried giving unicode string as
    input for the function something like:

    c = u"copy "+"\""+full_path+"\" "+dest
    os.system (c)
    First of all do not use os.system() to run external programs. You'll get
    in all sorts of trouble like quoting. You are far better of with the
    subprocess module.

    Second, why are you using an external program at all? The shutil module
    contains multiple functions to copy a file or directory.

    import shutil
    shutil.copy(source, dest)

    Christian
  • Venutaurus539 at Feb 25, 2009 at 5:28 am

    On Feb 25, 9:55?am, Christian Heimes wrote:
    venutaurus... at gmail.com schrieb:
    Hello,
    ? ? ? ? ?I am facing problems while using os.system() of python for
    copying a file from source to destination where the file name is in
    unicode (multi lingual characters).My python interpreter is trying to
    convert them to ascii and is failing. I tried giving unicode string as
    input for the function something like:
    c = u"copy "+"\""+full_path+"\" "+dest
    os.system (c)
    First of all do not use os.system() to run external programs. You'll get
    in all sorts of trouble like quoting. You are far better of with the
    subprocess module.

    Second, why are you using an external program at all? The shutil module
    contains multiple functions to copy a file or directory.

    import shutil
    shutil.copy(source, dest)

    Christian
    Thanks Christian for your reply,will definitely try.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 25, '09 at 4:20a
activeFeb 25, '09 at 5:28a
posts4
users2
websitepython.org

2 users in discussion

Venutaurus539: 3 posts Christian Heimes: 1 post

People

Translate

site design / logo © 2022 Grokbase