【发布时间】:2016-02-24 19:40:19
【问题描述】:
我正在尝试找到一种可靠的方法来使用 Python 扫描 Windows 上的文件,同时考虑文件名中可能存在各种 Unicode 代码点的可能性。我已经看到了几个针对这个问题提出的解决方案,但它们都不能解决我在扫描由现实世界的软件和用户创建的文件名时遇到的所有实际问题。
下面的代码示例试图解开和演示核心问题。它在一个子文件夹中创建了三个文件,其中包含我遇到的各种变体,然后尝试扫描该文件夹并显示每个文件名,后跟文件内容。它会在尝试读取第三个测试文件时崩溃,并带有 OSError [Errno 22] Invalid argument。
import os
# create files in .\temp that demonstrate various issues encountered in the wild
tempfolder = os.getcwd() + '\\temp'
if not os.path.exists(tempfolder):
os.makedirs(tempfolder)
print('file contents', file=open('temp/simple.txt','w'))
print('file contents', file=open('temp/with a ® symbol.txt','w'))
print('file contents', file=open('temp/with these chars ΣΑΠΦΩ.txt','w'))
# goal is to scan the files in a manner that allows for printing
# the filename as well as opening/reading the file ...
for root,dirs,files in os.walk(tempfolder.encode('UTF-8')):
for filename in files:
fullname = os.path.join(tempfolder.encode('UTF-8'), filename)
print(fullname)
print(open(fullname,'r').read())
正如代码中所说,我只想能够显示文件名并打开/读取文件。关于文件名的显示,我不在乎是否针对特殊情况正确呈现了 Unicode 字符。我只想以唯一标识正在处理的文件的方式打印文件名,并且不会为这些不寻常的文件名类型抛出错误。
如果您注释掉最后一行代码,此处显示的方法将显示所有三个文件名而没有错误。但它不会打开名称中包含杂项 Unicode 的文件。
是否有一种方法可以在 Python 中可靠地显示/打开所有这三种文件名变体?我希望有,但我对 Unicode 微妙之处的有限掌握使我无法看到它.
【问题讨论】:
-
你从哪里运行代码?
-
来自命令提示符,或来自 VS Code,在这两种情况下都出现相同的错误。完成后,我需要从命令提示符运行它。
-
@DougMahugh,使用 pycharm 或 cygwin 之类的 ide 可以省去很多麻烦,代码应该可以完美地运行并显示输出,cmd shell 在编码方面很麻烦。 cygwin.com, jetbrains.com/pycharm/download
-
@DougMahugh,您可以花几个小时尝试获得一个允许您使用 cmd shell 的解决方案,但您会发现它永远无法正常工作,或者您只需花 15 分钟进行设置cygwin 或支持 utf-8 的 ide 就可以了。
-
@roeland,'mbcs' 通常是不正确的,因为控制台默认使用 OEM 代码页,而不是 ANSI 代码页。使用
sys.stdout.encoding。
标签: python windows python-3.x unicode