我有一个需要在 Windows 上运行的 python 备份应用程序。它需要 UTF 兼容性(以便能够备份包含 UTF 字符(如意大利重音符号)的目录)。问题是它使用外部程序(plink、cygwin、ssh 和 rsync),我无法让它们工作。原型32行,请看:
# -*- coding: utf-8 -*-
import subprocess
def safestr(obj, encoding='utf-8'):
r"""Converts any given object to utf-8 encoded string.
>>> safestr('hello')
'hello'
>>> safestr(u'\u1234')
'\xe1\x88\xb4'
>>> safestr(2)
'2'
"""
if isinstance(obj, unicode):
return obj.encode("utf-8")
elif isinstance(obj, str):
return obj.encode
else:
return str(obj)
def execute(command):
pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
out, errs = pipe.communicate()
retcode = pipe.poll()
print "OUT: " + repr(out)
print "ERRS: " + repr(errs)
print "RET: " + str(retcode)
command = u'rsync --stats -az --numeric-ids --delete --blocking-io --modify-window=2 --no-group --chmod=u=rwX,g=,o= -e \'cygnative plink -ssh -2 -batch -pw test \' "/cygdrive/c/κόσμε" vaidab@192.168.1.86:/volatile/backup/vaidab/2010-03-03.15_41_56/ --link-dest=../2010-03-03.15_00_57'.encode('utf-8')
execute(command)
仍然不适用于 nosklo 的版本,检查结果:
python prototype_unicode_new.py 'rsync.exe --stats -az --numeric-ids --delete --blocking-io --modify-window=2 --no-group --chmod=u=rwX,g=,o = -e "cygnative plink -ssh -2 -batch -pw test" /cygdr ive/c/\xce\xba\xcf\x8c\xcf\x83\xce\xbc\xce\xb5 vaidab@192.168.1.86:/volatile /back kup/vaidab/2010-03-03.15_41_56/'
OUT: '\n文件数: 0\n传输的文件数: 0\n总文件大小: 0 字节\n总传输文件大小: 0 字节\n文字数据: 0 字节\n匹配数据: 0 字节\n文件列表大小: 9 \n文件列表生成时间:0.001 秒\n文件列表传输时间:0.000 秒\n发送的总字节数:22\n接收的总字节数:12\n\ nsent 接收的 22 个字节 12 字节 68.00 字节/秒\n总大小为 0 加速为 0.00\ n' ERRS: 'rsync: link_stat "/cygdrive/c/\xc3\x8e\xc2\xba\xc3\x8f\xc5\x92\xc3\x8f\xc 6\x92\xc3\x8e\xc2\xbc\xc3\ x8e\xc2\xb5" 失败:没有这样的文件或目录 (2)\nrs 同步错误:某些文件/属性未在 /home/lapo/packaging/rsync-3.0.6 传输(参见以前的错误)(代码 23) -1/src/rsync-3.0.6/main.c(1039) [发件人=3.0. 6]\n' RET: 23