python
主页 > 脚本 > python >

python 检查文件mime类型的方法

2018-12-10 | 秩名 | 点击:
今天小编给大家分享python 检查文件mime类型的方法。

magic 模块可以检查文件的mime类型,而不是从后缀名来判断,例如判断文件是不是视频或图片类型如下:


#检查文件类型
mime_type = magic.from_file(full_path,mime=True)
logger.info("上传的文件类型:"+str(mime_type))
if not mime_type.startswith('video') and not mime_type.startswith('image'):
 logger.error("非法的文件类型!")
 os.remove(full_path)
 return JsonResponse({'code':500,'msg':'非法的文件类型!'})



原文链接:
相关文章
最新更新