Is there a convenient way to map a file uri to os.path?

前端 未结 5 1347
栀梦
栀梦 2020-12-14 07:52

A subsystem which I have no control over insists on providing filesystem paths in the form of a uri. Is there a python module/function which can convert this path into the a

5条回答
  •  一生所求
    2020-12-14 08:12

    The solution from @Jakob Bowyer doesn't convert URL encoded characters to regular UTF-8 characters. For that you need to use urllib.parse.unquote.

    >>> from urllib.parse import unquote, urlparse
    >>> unquote(urlparse('file:///home/user/some%20file.txt').path)
    '/home/user/some file.txt'
    

提交回复
热议问题