Python:如何将具有unicode文件名的文件移动到unicode文件夹
在Windows下的Python脚本中,在unicode命名的文件夹之间移动unicode命名的文件让我感到非常痛苦 您将使用什么语法查找文件夹中所有*.ext类型的文件并将其移动到相对位置Python:如何将具有unicode文件名的文件移动到unicode文件夹,python,unicode,file-manipulation,Python,Unicode,File Manipulation,在Windows下的Python脚本中,在unicode命名的文件夹之间移动unicode命名的文件让我感到非常痛苦 您将使用什么语法查找文件夹中所有*.ext类型的文件并将其移动到相对位置 假设文件和文件夹是unicode。基本问题是unicode和字节字符串之间无法转换的混合。解决方案可以转换为单一格式,也可以使用一些技巧来避免问题。我的所有解决方案都包括glob和shutil标准库 例如,我有一些Unicode文件名以ods结尾,我想将它们移动到名为א的子目录(希伯来语Aleph,Unic
假设文件和文件夹是unicode。基本问题是unicode和字节字符串之间无法转换的混合。解决方案可以转换为单一格式,也可以使用一些技巧来避免问题。我的所有解决方案都包括
glob
和shutil
标准库
例如,我有一些Unicode文件名以ods
结尾,我想将它们移动到名为א
的子目录(希伯来语Aleph,Unicode字符)
第一个解决方案-将目录名称表示为字节字符串:
第二种解决方案-将文件名转换为Unicode:
归功于埃齐奥·梅洛蒂
第三种解决方案-避免目标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
更深层次的解释
简单的方法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)
>>files=glob.glob('*.ods')
>>>对于文件中的文件:
... copy2(文件,u'א')
...
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
文件“/usr/lib/python2.6/shutil.py”,第98行,在copy2中
dst=os.path.join(dst,os.path.basename(src))
文件“/usr/lib/python2.6/posixpath.py”,第70行,在join中
路径+='/'+b
UnicodeDecodeError:“ascii”编解码器无法解码位置1中的字节0xd7:
序号不在范围内(128)
如前所述,使用“א”
而不是Unicodeu“א”
这是虫子吗?
在我看来,这是一个bug,因为Python不能期望basedir
名称总是str
,而不是unicode
。我有,正在等待答复
进一步阅读
在任何地方使用Unicode字符串:
# -*- coding: utf-8 -*-
# source code ^^ encoding; it might be different from sys.getfilesystemencoding()
import glob
import os
srcdir = u'مصدر الدليل' # <-- unicode string
dstdir = os.path.join('..', u'κατάλογο προορισμού') # relative path
for path in glob.glob(os.path.join(srcdir, u'*.ext')):
newpath = os.path.join(dstdir, os.path.basename(path))
os.rename(path, newpath) # move file or directory; assume the same filesystem
#-*-编码:utf-8-*-
#源代码^^编码;它可能与sys.getfilesystemencoding()不同
导入glob
导入操作系统
srcdir=u'مددلللللللللللللللل!详细解释请参见+1
>>> 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)
# -*- coding: utf-8 -*-
# source code ^^ encoding; it might be different from sys.getfilesystemencoding()
import glob
import os
srcdir = u'مصدر الدليل' # <-- unicode string
dstdir = os.path.join('..', u'κατάλογο προορισμού') # relative path
for path in glob.glob(os.path.join(srcdir, u'*.ext')):
newpath = os.path.join(dstdir, os.path.basename(path))
os.rename(path, newpath) # move file or directory; assume the same filesystem