Search code examples
pythonspliturlparse

splitting a path with python


I am trying to get cut everything off after the last decimal and add "html" to the end

html

<a href="http://www.youversion.com/bible/gen.1.ceb">http://www.youversion.com/bible/gen.1.ceb</a>

current code returns "gen.1.ceb"

name = urlparse.urlparse(url).path.split('/')[-1]

I want name to get "gen.1.html"


Solution

  • You can do it like that:

    filename = urlparse.urlparse(url).path.split('/')[-1]  # get file name
    name = filename.rsplit('.', 1)[0] + '.html'  # change the extension