Get the mimetype of a file with Python

前端 未结 1 1950
Happy的楠姐
Happy的楠姐 2020-12-17 16:41

I want determine mimetype of an xml file , but I am getting error about some instance as first argument. I am new to python please help. Below is the code I am using and the

1条回答
  •  情话喂你
    2020-12-17 17:35

    The error says that you have to initialize the MimeTypes class:

    >>> from mimetypes import MimeTypes
    >>> import urllib 
    >>> 
    >>> mime = MimeTypes()
    >>> url = urllib.pathname2url('Upload.xml')
    >>> mime_type = mime.guess_type(url)
    >>> 
    >>> print mime_type
    ('application/xml', None)
    

    Although you could skip this and use mimetypes.guess_type directly:

    >>> import urllib, mimetypes
    >>> 
    >>> url = urllib.pathname2url('Upload.xml')
    >>> print mimetypes.guess_type(url)
    ('application/xml', None)
    

    0 讨论(0)
提交回复
热议问题