Convert a filename to a file:// URL

前端 未结 4 1631
眼角桃花
眼角桃花 2020-12-05 06:46

In WeasyPrint’s public API I accept filenames (among other types) for the HTML inputs. Any filename that works with the built-in open() should work, but I need

4条回答
  •  忘掉有多难
    2020-12-05 07:21

    Credit to comment from @danodonovan above.

    For Python3, the following code will work:

    from urllib.parse import urljoin
    from urllib.request import pathname2url
    
    def path2url(path):
        return urljoin('file:', pathname2url(path))
    

提交回复
热议问题