开发者

Python: Copying files with special characters in path [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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
pri开发者_运维问答nt "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'


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/


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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜