views:

306

answers:

3

Hi is there any possibility in Python 2.5 to copy files having special chars (Japanese chars, cyrillic letters) in their path? shutil.copy cannot handle this.

here is some example code:

import copy, os,shutil,sys
fname=os.getenv("USERPROFILE")+"\\Desktop\\testfile.txt"
print fname
print "type of fname: "+str(type(fname))
fname0 = unicode(fname,'mbcs')
print fname0
print "type of fname0: "+str(type(fname0))
fname1 = unicodedata.normalize('NFKD', fname0).encode('cp1251','replace')
print fname1
print "type of fname1: "+str(type(fname1))
fname2 = unicode(fname,'mbcs').encode(sys.stdout.encoding)
print fname2
print "type of fname2: "+str(type(fname2))

shutil.copy(fname2,'C:\\')

the output on a Russian Windows XP

C:\Documents and Settings\└фьшэшёЄЁрЄюЁ\Desktop\testfile.txt
type of fname: <type 'str'>
C:\Documents and Settings\Администратор\Desktop\testfile.txt
type of fname0: <type 'unicode'>
C:\Documents and Settings\└фьшэшёЄЁрЄюЁ\Desktop\testfile.txt
type of fname1: <type 'str'>
C:\Documents and Settings\Администратор\Desktop\testfile.txt
type of fname2: <type 'str'>
Traceback (most recent call last):
  File "C:\Test\getuserdir.py", line 23, in <module>
    shutil.copy(fname2,'C:\\')
  File "C:\Python25\lib\shutil.py", line 80, in copy
    copyfile(src, dst)
  File "C:\Python25\lib\shutil.py", line 46, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\\x80\
xa4\xac\xa8\xad\xa8\xe1\xe2\xe0\xa0\xe2\xae\xe0\\Desktop\\testfile.txt'
+1  A: 

Try passing unicode arguments to shutil.copy(). That is, shutil.copy( fname0, u'c:\\')

http://docs.python.org/howto/unicode.html#unicode-filenames

http://www.amk.ca/python/howto/unicode#unicode-filenames

http://www.python.org/dev/peps/pep-0277/

Forest
No, that does work. shutil.copy requires str arguments and converts them.result:IOError: [Errno 2] No such file or directory: u'C:\\Documents and Settings\\\u0410\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\\Desktop\\testfile.txt'
Interesting. I don't have a windows box handy. Would it be convenient for you to try it with python 2.6? I know there was some unicode work done for that release, but I don't know if it affected your problem.
Forest
The `shutil` functions don’t modify their arguments, and the `open` function always uses Unicode strings on Windows.
Philipp
A: 

As a workaround, you could os.chdir to the unicode-named directory so that shutil does not have to have Unicode arguments: (obviously that won't help you if you have non-ASCII in filenames.)

os.chdir(os.getenv("USERPROFILE")+"\\Desktop\\")
shutil.copy("testfile.txt",'C:\\')

Alternatively, you could copy files the good ole fashioned way.

in_file = open(os.getenv("USERPROFILE")+"\\Desktop\\testfile.txt", "rb")
out_file = open("C:\testfile.txt", "wb")
out_file.write(in_file.read())
in_file.close()
out_file.close()

A third workaround I can think of is using Python 3 instead :)

badp
A: 

Solved the problem

The Desktop path in Windows XP is not "C:\Documents and Settings\Администратор\Desktop". It is "C:\Documents and Settings\Администратор\Рабочий стол". And there is now mapping between both.

Since Windows Vista you can call this path with C:\users\Администратор\Desktop however it is called "C:\Пользователь\Администратор\Рабочий стол" in Explorer.