识别文件的格式
在linux中,我们有一个名为“file”的实用程序,它可以帮助我们确定文件的标识。有没有可以完成相同工作的 python 模块?
我不喜欢使用 subprocess.Popen(['file', 'blah.blah']), 因为它是平台相关的。例如,Windows没有“文件”(尽管可以下载)。
In linux, we have a utility called “file”, which helps us to determine the identification of a file. Is there any python module that can do the same job?
I don't prefer to use subprocess.Popen(['file', 'blah.blah'])
,
because it is platform dependent. For instance, windows do not have “file” (although it can be downloaded).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
魔法
不要忘记DLL。
magic
Don't forget the DLL.
Ignacio 提到了 Magic 库,但如果您确信文件名足以了解文件类型(通过扩展名),您也可以使用标准库来实现:
请参阅 Mimetypes 模块。然而,这并不能替代实际检查文件内容并确定其类型。
Ignacio mentioned the Magic library, but you could also do it with the standard library if you're confident that the filename is enough to know the file type from (by extension):
See the Mimetypes module in the standard library. This however isn't a replacement for actually inspecting the content of the file and figuring out its type.
分别有用于图形和声音文件的标准模块
imghdr
和sndhdr
。There are standard modules
imghdr
andsndhdr
for graphic and sound files, respectively.