且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查在Python中没有扩展名的文件类型?

更新时间:2022-11-28 18:07:14

There are Python libraries that can recognize files based on their content (usually a header / magic number) and that don't rely on the file name or extension.

If you're addressing many different file types, you can use python-magic. That's just a Python binding for the well-established magic library. This has a good reputation and (small endorsement) in the limited use I've made of it, it has been solid.

There are also libraries for more specialized file types. For example, the Python standard library has the imghdr module that does the same thing just for image file types.