How to guess the type of a file in python?

This question already has an answer here:

  • How to find the mime type of a file in python? 14 answers

  • 你想要filemagic模块。


    If you're on a Unix OS (Linux or Mac), you have access to magic . If on a Mac, you'll likely need to brew install libmagic . There's a Python library called filemagic for rolling it into your Python scripts.

    import magic
    mage = magic.Magic()
    mage.id_buffer("adsfadsf←")
    

    The last line will return 'UTF-8 Unicode text, with no line terminators'

    You can also have it check files, which isn't based on the filename but rather the magic bytes at the start of the file:

    法师检查

    链接地址: http://www.djcxy.com/p/46792.html

    上一篇: 在Python中,你如何获取内容

    下一篇: 如何猜测在Python中的文件的类型?