Python get characters after last slash

How can I extract whatever follows the last slash in a URL in Python? For example, these URLs should return the following:

URL: http://www.test.com/TEST1
returns: TEST1

URL: http://www.test.com/page/TEST2
returns: TEST2

URL: http://www.test.com/page/page/12345
returns: 12345

I've tried urlparse, but that gives me the full path filename, such as page/page/12345.

Python get characters after last slash

Remi Guan

20.5k17 gold badges62 silver badges83 bronze badges

asked Aug 31, 2011 at 7:23

Python get characters after last slash

5

You don't need fancy things, just see the string methods in the standard library and you can easily split your url between 'filename' part and the rest:

url.rsplit('/', 1)

So you can get the part you're interested in simply with:

url.rsplit('/', 1)[-1]

Python get characters after last slash

Remi Guan

20.5k17 gold badges62 silver badges83 bronze badges

answered Aug 31, 2011 at 7:28

Luke404Luke404

9,8423 gold badges24 silver badges31 bronze badges

6

One more (idio(ma)tic) way:

URL.split("/")[-1]

answered Aug 31, 2011 at 7:31

KimvaisKimvais

37.2k16 gold badges106 silver badges140 bronze badges

2

rsplit should be up to the task:

In [1]: 'http://www.test.com/page/TEST2'.rsplit('/', 1)[1]
Out[1]: 'TEST2'

answered Aug 31, 2011 at 7:28

0

You can do like this:

head, tail = os.path.split(url)

Where tail will be your file name.

answered Sep 20, 2013 at 13:53

neowinstonneowinston

7,41610 gold badges51 silver badges83 bronze badges

1

urlparse is fine to use if you want to (say, to get rid of any query string parameters).

import urllib.parse

urls = [
    'http://www.test.com/TEST1',
    'http://www.test.com/page/TEST2',
    'http://www.test.com/page/page/12345',
    'http://www.test.com/page/page/12345?abc=123'
]

for i in urls:
    url_parts = urllib.parse.urlparse(i)
    path_parts = url_parts[2].rpartition('/')
    print('URL: {}\nreturns: {}\n'.format(i, path_parts[2]))

Output:

URL: http://www.test.com/TEST1
returns: TEST1

URL: http://www.test.com/page/TEST2
returns: TEST2

URL: http://www.test.com/page/page/12345
returns: 12345

URL: http://www.test.com/page/page/12345?abc=123
returns: 12345

answered Apr 4, 2013 at 5:51

Jacob WanJacob Wan

2,33321 silver badges19 bronze badges

2

os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/'))
>>> folderD

answered Jan 15, 2019 at 5:01

RochanRochan

1,2841 gold badge13 silver badges15 bronze badges

2

Here's a more general, regex way of doing this:

    re.sub(r'^.+/([^/]+)$', r'\1', url)

answered Apr 12, 2018 at 14:32

sandoronodisandoronodi

3162 silver badges11 bronze badges

1

First extract the path element from the URL:

from urllib.parse import urlparse
parsed= urlparse('https://www.dummy.example/this/is/PATH?q=/a/b&r=5#asx')

and then you can extract the last segment with string functions:

parsed.path.rpartition('/')[2]

(example resulting to 'PATH')

answered Sep 19, 2011 at 9:22

tzottzot

89k29 gold badges136 silver badges200 bronze badges

2

Use urlparse to get just the path and then split the path you get from it on / characters:

from urllib.parse import urlparse

my_url = "http://example.com/some/path/last?somequery=param"
last_path_fragment = urlparse(my_url).path.split('/')[-1]  # returns 'last'

Note: if your url ends with a / character, the above will return '' (i.e. the empty string). If you want to handle that case differently, you need to strip the last trailing / character before you split the path:

my_url = "http://example.com/last/"
# handle URL ending in `/` by removing it.
last_path_fragment = urlparse(my_url).path.rstrip('/', 1).split('/')[-1]  # returns 'last'

answered Nov 18, 2020 at 22:01

The following solution, which uses pathlib to parse the path obtained from urllib.parse allows to get the last part even when a terminal slash is present:

import urllib.parse
from pathlib import Path

urls = [
    "http://www.test.invalid/demo",
    "http://www.test.invalid/parent/child",
    "http://www.test.invalid/terminal-slash/",
    "http://www.test.invalid/query-params?abc=123&works=yes",
    "http://www.test.invalid/fragment#70446893",
    "http://www.test.invalid/has/all/?abc=123&works=yes#70446893",
]

for url in urls:
    url_path = Path(urllib.parse.urlparse(url).path)
    last_part = url_path.name  # use .stem to cut file extensions
    print(f"{last_part=}")

yields:

last_part='demo'
last_part='child'
last_part='terminal-slash'
last_part='query-params'
last_part='fragment'
last_part='all'

answered Dec 22, 2021 at 9:32

extracted_url = url[url.rfind("/")+1:];

answered Aug 31, 2011 at 7:28

fardjadfardjad

19.4k6 gold badges50 silver badges67 bronze badges

0

Split the url and pop the last element url.split('/').pop()

answered May 19, 2017 at 9:16

Python get characters after last slash

Atul YadavAtul Yadav

1,9721 gold badge13 silver badges15 bronze badges

Split the URL and pop the last element

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];

console.log(plants.pop());
// expected output: "tomato"

console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]

answered Jun 10, 2021 at 8:58

Python get characters after last slash

Jaimin PatelJaimin Patel

4,4113 gold badges31 silver badges35 bronze badges

url ='http://www.test.com/page/TEST2'.split('/')[4]
print url

Output: TEST2.

sigod

2,3712 gold badges24 silver badges42 bronze badges

answered Feb 18, 2013 at 21:42

live_alonelive_alone

1631 silver badge11 bronze badges

1

How do you get everything after the last slash in Python?

To get everything after the last slash in a string:.
Use the str. rsplit() method to split the string on a slash, from the right..
Get the list element at index 1 ..
The method will return a new string that only contains the part after the last slash..

How do you get the string after the last slash?

First, find the last index of ('/') using . lastIndexOf(str) method. Use the . substring() method to get the access the string after last slash.

How do I get the last substring in Python?

The last character of a string has index position -1. So, to get the last character from a string, pass -1 in the square brackets i.e. It returned a copy of the last character in the string. You can use it to check its content or print it etc.

How do you split a string with a forward slash in Python?

Use the str. split() method to split a string on the forward slashes, e.g. my_list = my_str. split('/') .