-1

How to use urllib.parse in python to modify the path of URI? I know I can use substring and index but I am wondering what is the right way to accomplish this.

Convert this:

https://foo.blob.bar.storage.azure.net/container/blobname?sv=2015-04-05&ss=123&srt=abc&sp=efg&se=2022&sig=wsx

To this:

https://foo.blob.bar.storage.azure.net/container?sv=2015-04-05&ss=123&srt=abc&sp=efg&se=2022&sig=wsx

5
  • Python's string.replace() won't do? Commented May 6, 2022 at 16:46
  • can we parse the uri and mofiy the path and rebuild the uri? Commented May 6, 2022 at 16:47
  • @Aziz not sure how string.replace could be used without assuming last backslash doesn't happen in the query part of uri as well Commented May 6, 2022 at 16:51
  • I assumed that /blobname would be the part to remove. I've posted an answer with details Commented May 6, 2022 at 16:59
  • Does this answer your question? Is there a better way to write this URL Manipulation in Python? Commented May 6, 2022 at 17:05

1 Answer 1

0

If you're just trying to remove /blobname from the URL, you can simply use:

url = 'https://foo.blob.bar.storage.azure.net/container/blobname?sv=2015-04-05&ss=123&srt=abc&sp=efg&se=2022&sig=wsx'
new_url = url.replace('/blobname', '')

However, if you really want to parse the URL and reconstruct it (let's say you want to go up one directory in the URL path), you can do this:

from urllib.parse import urlparse, urlunparse
from pathlib import Path

url = 'https://foo.blob.bar.storage.azure.net/container/blobname?sv=2015-04-05&ss=123&srt=abc&sp=efg&se=2022&sig=wsx'

u = urlparse(url)
url_parts = list(u)   # Need to convert to list to update the path item
url_parts[2] = str(Path(u.path).parent)   # Using Path lib to go to the parent path
new_url = urlunparse(url_parts)  # create a new URL 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.