Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python split url to find image name and extension

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"
like image 863
ApPeL Avatar asked May 11 '12 13:05

ApPeL


People also ask

How do I separate an image name and extension in Python?

Use the os. path. splitext() method to split a filename on the name and extension, e.g. filename, extension = os. path.

How do you split a string by URL in Python?

Method #1 : Using split() ' and return the first part of split for result.

How do I get filenames without an extension in Python?

Get filename from the path without extension using rsplit() Python String rsplit() method returns a list of strings after breaking the given string from the right side by the specified separator.

How do I strip a file extension in Python?

Remove Extension From Filename in Python Using the os Module. Given a file name, we can remove the file extension using the os. path. splitext() function.


2 Answers

# Here's your link:
picture_page = "http://distilleryimage2.instagram.com/da4ca3509a7b11e19e4a12313813ffc0_7.jpg"

#Here's your filename and ext:
filename, ext = (picture_page.split('/')[-1].split('.'))

When you do picture_page.split('/'), it will return a list of strings from your url split by a /. If you know python list indexing well, you'd know that -1 will give you the last element or the first element from the end of the list. In your case, it will be the filename: da4ca3509a7b11e19e4a12313813ffc0_7.jpg

Splitting that by delimeter ., you get two values: da4ca3509a7b11e19e4a12313813ffc0_7 and jpg, as expected, because they are separated by a period which you used as a delimeter in your split() call.

Now, since the last split returns two values in the resulting list, you can tuplify it. Hence, basically, the result would be like:

filename,ext = ('da4ca3509a7b11e19e4a12313813ffc0_7', 'jpg')

like image 120
bad_keypoints Avatar answered Sep 27 '22 19:09

bad_keypoints


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.

like image 27
Christian Witts Avatar answered Sep 27 '22 18:09

Christian Witts