在我的小项目中,我需要识别目录中的文件类型。所以我使用了
你可以观察到,
python-magic
模块,并做了以下操作:from Tkinter import Tk
from tkFileDialog import askdirectory
def getDirInput():
root = Tk()
root.withdraw()
return askdirectory()
di = getDirInput()
print('Selected Directory: ' + di)
for f in os.listdir(di):
m = magic.Magic(magic_file='magic')
print 'Type of ' + f + ' --> ' + m.from_file(f)
但是似乎 python-magic
在我将Unicode文件名传递给 from_file()
函数时无法处理它。以下是一个示例输出:
Selected Directory: C:/Users/pruthvi/Desktop/vidrec/temp
Type of log.txt --> ASCII English text, with very long lines, with CRLF, CR line terminators
Type of TAEYEON 태연_ I (feat. Verbal Jint)_Music Video.mp4 --> cannot open `TAEYEON \355\234\227_ I (feat. Verbal Jint)_Music Video.mp4' (No such file or directory)
Type of test.py --> a python script text executable
你可以观察到,
python-magic
无法识别第二个文件 TAEYEON...
的类型,因为其中包含 Unicode 字符。它将 태연
字符显示为 \355\234\227
,而我在两种情况下都传递了相同的字符。我该如何解决这个问题并找到具有 Unicode 字符的文件类型?谢谢。
m.from_file(os.path.join(di, f))
- falsetru