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

前端 未结 19 1162
猫巷女王i
猫巷女王i 2020-11-22 15:19

Let\'s say you want to save a bunch of files somewhere, for instance in BLOBs. Let\'s say you want to dish these files out via a web page and have the client automatically o

19条回答
  •  长发绾君心
    2020-11-22 15:41

    I 've tried a lot of examples but with Django mutagen plays nicely.

    Example checking if files is mp3

    from mutagen.mp3 import MP3, HeaderNotFoundError  
    
    try:
        audio = MP3(file)
    except HeaderNotFoundError:
        raise ValidationError('This file should be mp3')
    

    The downside is that your ability to check file types is limited, but it's a great way if you want not only check for file type but also to access additional information.

提交回复
热议问题