基本问题是Unipre和字节字符串之间未转换的混合。解决方案可以转换为单一格式,也可以避免一些麻烦而避免出现问题。我所有的解决方案都包含
glob和
shutil标准库。
举例来说,我有一些以结尾的Unipre文件名
ods,我想将它们移动到名为
א(希伯来语Aleph,一个Unipre字符)的子目录中。
第一种解决方案-将目录名称表示为字节字符串:
>>> import glob>>> import shutil>>> files=glob.glob('*.ods') # List of Byte string file names>>> for file in files:... shutil.copy2(file, 'א') # Byte string directory name...第二种解决方案-将文件名转换为Unipre:
>>> import glob>>> import shutil>>> files=glob.glob(u'*.ods') # List of Unipre file names>>> for file in files:... shutil.copy2(file, u'א') # Unipre directory name
感谢Ezio Melotti,Python错误列表。
第三种解决方案-避免目标Unipre目录名称
尽管这并不是我认为的最佳解决方案,但这里有个不错的技巧值得一提。
使用将目录更改为目标目录
os.getcwd(),然后通过将其引用为来将文件复制到该目录
.:
# -*- coding: utf-8 -*-import osimport shutilimport globos.chdir('א') # CD to the destination Unipre directoryprint os.getcwd() # DEBUG: Make sure you're in the right placefiles=glob.glob('../*.ods') # List of Byte string file namesfor file in files: shutil.copy2(file, '.') # Copy each file# Don't forget to go back to the original directory here, if it matters更深入的解释
直接方法
shutil.copy2(src, dest)失败了,因为
shutil用ASCII字符串连接了Unipre而不进行转换:
>>> files=glob.glob('*.ods')>>> for file in files:... shutil.copy2(file, u'א')... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/usr/lib/python2.6/shutil.py", line 98, in copy2 dst = os.path.join(dst, os.path.basename(src)) File "/usr/lib/python2.6/posixpath.py", line 70, in join path += '/' + bUnipreDepreError: 'ascii' prec can't depre byte 0xd7 in position 1: ordinal not in range(128)如前所述,使用
'א'而不是Unipre可以避免这种情况
u'א'
这是一个错误吗?
在我看来,这是一个错误,因为Python无法期望
basedir名称始终为
str,而不是
unipre。我在Python
Buglist中报告了这个问题,并等待响应。
进一步阅读
Python的官方Unipre
HOWTO



