how to find that file is image or document or ... without extension and content type?
<h1>File Manager</h1>

<p>I want uploading any file and i have a file manager service that get the file and saving without extension and files name are UUID and return file information.</p>

<p>my file manager handler :</p>

<pre><code>#!/usr/bin/python
# -*- coding: utf-8 -*-

import json
from pyramid_storage.exceptions import FileNotAllowed
import uuid
from pyramid.view import view_config
from pyramid.response import Response
import os


class UploadHandler:
    def __init__(self, request):
        self.request = request
        self.settings = self.request.registry.settings

    @view_config(route_name='upload', request_method='POST', renderer='json')
    def post(self):
        # file
        f = self.request.POST.items()

        # file name
        file_name_main = f[0][1].filename

        # content type
        content_type = str(f[0][1].type)

        if content_type:
            extension_main = content_type.split('.')[-1]

            # set id for file name
            if extension_main:
                f[0][1].filename = str(uuid.uuid4())
            else:
                response = Response(body=json.dumps({'ERROR': 'Your File Not Allowed'}))
                response.headers.update({
                    'Access-Control-Allow-Origin': self.settings['Access-Control-Allow-Origin'],
                })
                return response
        else:
            response = Response(body=json.dumps({'ERROR': 'Your File Not Allowed'}))
            response.headers.update({
                'Access-Control-Allow-Origin': self.settings['Access-Control-Allow-Origin'],
            })
            return response

        try:
            # save file
            file_name = str(self.request.storage.save(f[0][1]))
        except FileNotAllowed:         
            response = Response(body=json.dumps({'ERROR': 'Your File Not Allowed'}))
            response.headers.update({
                'Access-Control-Allow-Origin': self.settings['Access-Control-Allow-Origin'],
            })
            return response

        # file name == file id
        f_name = file_name
        file_path = self.request.storage.base_path + os.sep + f_name
        file_size = os.path.getsize(file_path)

        response = Response(body=json.dumps(
            {'file_id': f_name, 'file_name': file_name_main, 'content_type': content_type, 'size': file_size, 
             'extension': extension_main}))

        response.headers.update({
            'Access-Control-Allow-Origin': self.settings['Access-Control-Allow-Origin'],
        })
        return response
</code></pre>

<p>There's a UNIX utility called <code>file</code> that uses "magic" to recognize known file types.  <code>file</code> uses a library called <code>libmagic</code> for this purpose.</p>

<p>The python interface to <code>libmagic</code> is called <code>filemagic</code> and you can get it <a href="https://pypi.python.org/pypi/filemagic/" rel="nofollow">here</a>.</p>

python file-upload