Skip to content
Advertisement

Save the name of an image from a url

I need to find a way to get the image name of any given image from a url. For example, in “…/images/foo.png”, the image name is “foo.png”. Is there a way to read this url and save just the name of the image up until the “/”? Thanks!

Advertisement

Answer

You can just use split:

url = 'https://upload.wikimedia.org/wikipedia/en/d/d0/Dogecoin_Logo.png'
*_, filename = url.split('/')
print(filename)
# Outputs Dogecoin_Logo.png
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement