Python:如何将具有unicode文件名的文件移动到unicode文件夹

Jon*_*han 7 python unicode file-manipulation

我想在Windows下的Python脚本中在unicode命名文件夹之间移动一个名为unicode的文件...

您将使用什么语法来查找文件夹中*.ext类型的所有文件并将它们移动到相对位置?

假设文件和文件夹是unicode.

Ada*_*tan 15

基本问题是Unicode和字节字符串之间的未转换混合.解决方案可以转换为单一格式或使用一些技巧避免问题.我所有的解决方案,包括globshutil标准库.

为了举例,我有一些以.结尾的Unicode文件名ods,我想将它们移动到名为?(Hebrew Aleph,一个unicode字符)的子目录中.

第一个解决方案 - 将目录名称表示为字节串:

>>> 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
... 
Run Code Online (Sandbox Code Playgroud)

第二种解决方案 - 将文件名转换为Unicode:

>>> import glob
>>> import shutil
>>> files=glob.glob(u'*.ods')     # List of Unicode file names
>>> for file in files:
...     shutil.copy2(file, u'?')  # Unicode directory name
Run Code Online (Sandbox Code Playgroud)

感谢Ezio Melotti,Python错误列表.

第三种解决方案 - 避免目标Unicode目录名称

虽然这不是我认为最好的解决方案,但这里有一个值得一提的好方法.

使用os.getcwd(),将目录更改为目标目录,然后通过引用将其复制到目标目录.:

# -*- coding: utf-8 -*-
import os
import shutil
import glob

os.chdir('?')                   # CD to the destination Unicode directory
print os.getcwd()               # DEBUG: Make sure you're in the right place
files=glob.glob('../*.ods')     # List of Byte string file names
for file in files:
        shutil.copy2(file, '.') # Copy each file
# Don't forget to go back to the original directory here, if it matters
Run Code Online (Sandbox Code Playgroud)

更深入的解释

直接的方法shutil.copy2(src, dest)失败,因为shutil在没有转换的情况下将unicode与ASCII字符串连接起来:

>>> 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 += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 1: 
                    ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

如前所述,使用'?'而不是Unicode 时可以避免这种情况u'?'

这是一个错误吗?

在我看来,这是错误,因为Python不能指望basedir名称总是str,而不是unicode.我已将此问题报告为Python错误列表中的问题,并等待响应.

进一步阅读

Python的官方Unicode HOWTO