This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Make mimetypes.guess_type accept path-like objects
Type: Stage: resolved
Components: Library (Lib) Versions:
Status: closed Resolution: duplicate
Dependencies: Superseder: Allow querying a Path's mime-type
View: 34926
Assigned To: Nosy List: Valentin.Lorentz, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-05-04 16:08 by Valentin.Lorentz, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg292995 - (view) Author: ProgVal (Valentin.Lorentz) Date: 2017-05-04 16:08
The documentation for mimetypes.guess_type says that it “guesses the type of a file based on its filename or URL”.

However, this function only accepts a string object, and not a bytes object:

>>> import os
>>> import mimetypes
>>> mimetypes.guess_type(os.listdir(os.fsencode('./'))[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/", line 291, in guess_type
    return _db.guess_type(url, strict)
  File "/usr/lib/python3.7/", line 116, in guess_type
    scheme, url = urllib.parse.splittype(url)
  File "/usr/lib/python3.7/urllib/", line 924, in splittype
    match = _typeprog.match(url)
TypeError: cannot use a string pattern on a bytes-like object
msg293106 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-05-05 13:38
Why do you think this is a bug?
msg293107 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-05-05 13:40
To clarify my question, I don't think it is a bug, but the fact that you do might mean there needs to be a doc clarification.  Or it might not.  Or, there might be a desirable enhancement here, maybe relating to the pathlike protocol.
msg293108 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-05 14:04
There were issues with mimetypes.guess_type() on Windows in Python 2 (issue9291 and a number of duplicate issues). I afraid that adding support of bytes paths will return the hell from which we ran in Python 3.
msg293115 - (view) Author: ProgVal (Valentin.Lorentz) Date: 2017-05-05 15:59
@r.david.murray I don't think it's a bug either, but a possible enhancement.

@serhiy.storchaka We could simply do this: if isinstance(url, bytes):  url = os.fsdecode(url)
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74454
2022-01-18 09:28:49iritkatrielsetstatus: open -> closed
resolution: duplicate
superseder: Allow querying a Path's mime-type
stage: resolved
2017-05-05 15:59:37Valentin.Lorentzsetmessages: + msg293115
2017-05-05 14:04:02serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg293108
2017-05-05 13:40:15r.david.murraysetmessages: + msg293107
2017-05-05 13:38:09r.david.murraysetnosy: + r.david.murray
messages: + msg293106
2017-05-04 16:08:06Valentin.Lorentzcreate