Python split url to find image name and extension

django, file-io, python

Solution

try:
    # Python 3
    from urllib.parse import urlparse
except ImportError:
    # Python 2
    from urlparse import urlparse
from os.path import splitext, basename

picture_page = "http://distilleryimage2.instagram.com/da4ca3509a7b11e19e4a12313813ffc0_7.jpg"
disassembled = urlparse(picture_page)
filename, file_ext = splitext(basename(disassembled.path))

Only downside with this is that your filename will contain a preceding / which you can always remove yourself.

Problem

I am looking for a way to extract a filename and extension from a particular url using Python lets say a URL looks as follows ``` picture_page = "http://distilleryimage2.instagram.com/da4ca3509a7b11e19e4a12313813ffc0_7.jpg" ``` How would I go about getting the following. ``` filename = "da4ca3509a7b11e19e4a12313813ffc0_7" file_ext = ".jpg" ```

Original source