Skip to content Skip to sidebar Skip to footer

Get The Mimetype Of A File With Python

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 error

Solution 1:

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)

Post a Comment for "Get The Mimetype Of A File With Python"