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':'非法的文件类型!'})

以上这篇python 检查文件mime类型的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Python使用filetype精确判断文件类型

    filetype.py Small and dependency free Python package to infer file type and MIME type checking the  magic numbers signature of a file or buffer. This is a Python port from filetype Go package. Works in Python  +3 . 一个小巧自由开放Python开发包,主要用来获得文件类型.包要求Pyt

  • Python中实现从目录中过滤出指定文件类型的文件

    最近学习下python,将从指定目录中过滤出指定文件类型的文件输出的方法总结一下,供日后查阅 复制代码 代码如下: #!/usr/bin/env python import glob import os os.chdir("./") for file in glob.glob("*.py"): print file print "#######Another One##########" for file in os.listdir("

  • Python判断文件或文件夹是否存在的三种方法

    常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错.所以最好在做任何操作之前,先判断文件是否存在. 这里将介绍三种判断文件或文件夹是否存在的方法,分别使用os模块.Try语句.pathlib模块. 1.使用os模块 os模块中的os.path.exists()方法用于检验文件是否存在. 判断文件是否存在 import os os.path.exists(test_file.txt) #True os.path.exists(no_exist_file.txt) #Fa

  • Python判断文件和文件夹是否存在的方法

    一.python判断文件和文件夹是否存在.创建文件夹 复制代码 代码如下: >>> import os >>> os.path.exists('d:/assist') True >>> os.path.exists('d:/assist/getTeacherList.py') True >>> os.path.isfile('d:/assist') False >>> os.path.isfile('d:/assis

  • 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("非

  • php实现获取文件mime类型的方法

    本文实例讲述了php获取文件mime类型的方法.分享给大家供大家参考.具体如下: 1.使用 mime_content_type 方法 string mime_content_type ( string $filename ) Returns the MIME content type for a file as determined by using information from the magic.mime file. <?php $mime_type = mime_content_typ

  • php准确获取文件MIME类型的方法

    本文实例讲述了php准确获取文件MIME类型的方法.分享给大家供大家参考.具体实现方法如下: <?php $mime = array ( //applications 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'doc' => 'application/vnd.ms-word', 'xls' => '

  • PHP实现获取文件mime类型多种方法解析

    本文实例讲述了php获取文件mime类型的方法.分享给大家供大家参考.具体如下: 1.使用 mime_content_type 方法 string mime_content_type ( string $filename ) Returns the MIME content type for a file as determined by using information from the magic.mime file. <?php $mime_type = mime_content_typ

  • SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享

    解决方案如下,其它框架雷同. 源代码(/system/libraries/upload.php 199 line) $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']); 修改成如下: 复制代码 代码如下: //Edit By Tacker if(function_exists('mime_content_type')){ $this->file_t

  • python 设置文件编码格式的实现方法

    如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,否则python2会默认使用ASCII编码.(python3已经没有这个问题了,python3默认的文件编码是UTF-8) 必须将编码注释放在第一行或者第二行,一般来说,Python文件的前两行要这样写: #!/usr/bin/python # -*- coding: UTF-8 -*- 其中第一行是指定python解释器,第二行是指定python文件编码方式,设置编码方式有以下可选的方法 1. 带等号的设置方法:

  • 对python .txt文件读取及数据处理方法总结

    1.处理包含数据的文件 最近利用Python读取txt文件时遇到了一个小问题,就是在计算两个np.narray()类型的数组时,出现了以下错误: TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U3') dtype('<U3') dtype('<U3') 作为一个Python新手,遇到这个问题后花费了挺多时间,在网上找了许多大神们写的例子,最后终于解决了. 总

  • JavaScript 检测文件的类型的方法

    我们会想到通过 input 元素的 accept 属性来限制上传的文件类型: <input type="file" id="inputFile" accept="image/png" /> 这种方案虽然可以满足大多数场景,但如果用户把 JPEG 格式的图片后缀名更改为 .png 的话,就可以成功突破这个限制.那么应该如何解决这个问题呢?其实我们可以通过读取文件的二进制数据来识别正确的文件类型.在介绍具体的实现方案前,阿宝哥先以图片类型

  • Python获取文件ssdeep值的方法

    本文实例讲述了Python获取文件ssdeep值的方法,分享给大家供大家参考.具体方法如下: 首先,得到ssdeep值,需要先import ssdeep 在ubuntu上安装pyssdeep时 一直出错  后来发现apt-cache search "ssdeep"时把几个全apt-get install 上,但问题依旧. 后来下载到pyssdeep的源文件 ,tar zxvf pyssdeep.tar.zip 然后 apt-get install python-dev 然后 pytho

  • python获取文件扩展名的方法

    本文实例讲述了python获取文件扩展名的方法.分享给大家供大家参考.具体实现方法如下: import os.path def file_extension(path): return os.path.splitext(path)[1] print file_extension('C:\py\wxPython.gif') 输出结果为: .gif 希望本文所述对大家的Python程序设计有所帮助.

随机推荐