Title: Allow querying a Path's mime-type
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pitrou Nosy List: YoSTEALTH, pitrou, rhettinger, xtreak
Priority: normal Keywords: easy, patch

Created on 2018-10-08 06:07 by YoSTEALTH, last changed 2018-10-12 07:00 by pitrou. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9777 merged python-dev, 2018-10-09 18:22
Messages (7)
msg327323 - (view) Author: (YoSTEALTH) * Date: 2018-10-08 06:07
How about adding basic "mime_type" method to "pathlib.Path" ?

Code would do something like:

    import mimetypes

    def mime_type(name):
        """Mime-type of the file."""
        find = name.rfind('.')
        ext = '' if find == -1 else name[find:]
        return mimetypes.types_map.get(ext, 'application/octet-stream')

Users would use it like so:

    import pathlib

    file = pathlib.Path('')
    print(file.mime_type)  # 'text/x-python'
msg327377 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-10-09 01:03
-0 There is some value in having a separation of responsibilities and in not adding another dependency.  On the other hand, I can see how this would sometimes be convenient.

Assigning to Antoine to make the decision.
msg327382 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2018-10-09 06:10
Thanks for the report. There was a similar request to add pathlib.rmtree (issue33498) and adding more methods as Raymond mentioned might be convenient but there is a maintenance cost (msg316517) since there is a consensus that pathlib should do everything os.path does. It's up to Antoine to decide on this.
msg327383 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-10-09 08:08
I would rather have the mimetypes module improved to accept Path-like objects, so that the following works:

>>> p = Path('LICENSE.txt')
>>> mimetypes.guess_type(p)
('text/plain', None)

It should be quite simple to implement as well.
msg327478 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-10-10 14:46
New changeset 7e18deef652a9d413d5dbd19d61073ba7eb5460e by Antoine Pitrou (Mayank Asthana) in branch 'master':
bpo-34926: Make mimetypes.guess_type accept os.PathLike objects (GH-9777)
msg327565 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2018-10-12 06:49
As part of triaging, I am closing this since this was merged with doc changes and there was no back port done since it's an enhancement. Antoine, feel free to reopen this if there are any changes left.

Thanks everybody for the feedback and review.
msg327566 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-10-12 07:00
No, I simply forgot to close it. Thank you!
Date User Action Args
2018-10-12 07:00:45pitrousetmessages: + msg327566
2018-10-12 06:49:13xtreaksetstatus: open -> closed
resolution: fixed
messages: + msg327565

stage: patch review -> resolved
2018-10-10 14:46:55pitrousetmessages: + msg327478
2018-10-09 18:22:33python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request9163
2018-10-09 08:09:02pitrousettitle: Adding "mime_type" method to pathlib.Path -> Allow querying a Path's mime-type
2018-10-09 08:08:41pitrousetkeywords: + easy

messages: + msg327383
components: + Library (Lib)
2018-10-09 06:10:59xtreaksetnosy: + xtreak
messages: + msg327382
2018-10-09 01:03:03rhettingersetassignee: pitrou

messages: + msg327377
nosy: + rhettinger, pitrou
2018-10-08 22:33:56YoSTEALTHsetcomponents: - Extension Modules
2018-10-08 06:46:34xtreaksettitle: Adding "mine_type" method to pathlib.Path -> Adding "mime_type" method to pathlib.Path
2018-10-08 06:07:23YoSTEALTHcreate