3

Is there a way in Python 2.5 to copy files which have 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'
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
user351681
  • 31
  • 1
  • 2
  • What happens on `shutil.copy(fname, "C:\\")`? It looks like you may be encoding that which doesn't need to be encoded. – msw May 27 '10 at 07:30
  • output: 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' – user351681 May 27 '10 at 08:00
  • On Ubuntu Linux, `shutil.copy("Администратор/boo", "Администратор/foo")` works with no problem whatsoever in Python 2.6 :/ – badp May 27 '10 at 08:33
  • I guess this is a problem on Windows XP Sp3. The case works fine on a Russian or Japanese Windows 7. – user351681 May 27 '10 at 11:25

3 Answers3

2

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/

ʇsәɹoɈ
  • 22,757
  • 7
  • 55
  • 61
  • 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' – user351681 May 27 '10 at 08:23
  • 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. – ʇsәɹoɈ May 27 '10 at 08:46
  • The `shutil` functions don’t modify their arguments, and the `open` function always uses Unicode strings on Windows. – Philipp May 27 '10 at 09:32
0

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
  • 11,409
  • 3
  • 61
  • 89
0

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.

user351681
  • 31
  • 1
  • 2