[python] How to get everything after last slash in a URL?

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.

This question is related to python parsing url

The answer is

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]

One more (idio(ma)tic) way:


rsplit should be up to the task:

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

You can do like this:

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

Where tail will be your file name.

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

import urllib.parse

urls = [

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]))


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

>>> folderD

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

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

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 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('/').split('/')[-1]  # returns 'last'

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:


(example resulting to 'PATH')

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

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

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

Output: TEST2.

Questions with python tag:

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation Upgrade to python 3.8 using conda Unable to allocate array with shape and data type How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip How to prevent Google Colab from disconnecting? "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm How to fix 'Object arrays cannot be loaded when allow_pickle=False' for imdb.load_data() function? "E: Unable to locate package python-pip" on Ubuntu 18.04 Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session' Jupyter Notebook not saving: '_xsrf' argument missing from post How to Install pip for python 3.7 on Ubuntu 18? Python: 'ModuleNotFoundError' when trying to import module from imported package OpenCV TypeError: Expected cv::UMat for argument 'src' - What is this? Requests (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.") Error in PyCharm requesting website How to setup virtual environment for Python in VS Code? Pylint "unresolved import" error in Visual Studio Code Pandas Merging 101 Numpy, multiply array with scalar What is the meaning of "Failed building wheel for X" in pip install? Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed Could not install packages due to an EnvironmentError: [Errno 13] OpenCV !_src.empty() in function 'cvtColor' error ConvergenceWarning: Liblinear failed to converge, increase the number of iterations How to downgrade python from 3.7 to 3.6 I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? Iterating over arrays in Python 3 How do I install opencv using pip? How do I install Python packages in Google's Colab? How do I use TensorFlow GPU? How to upgrade Python version to 3.7? How to resolve TypeError: can only concatenate str (not "int") to str How can I install a previous version of Python 3 in macOS using homebrew? Flask at first run: Do not use the development server in a production environment TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array What is the difference between Jupyter Notebook and JupyterLab? Pytesseract : "TesseractNotFound Error: tesseract is not installed or it's not in your path", how do I fix this? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'" How do I resolve a TesseractNotFoundError? Trying to merge 2 dataframes but get ValueError Authentication plugin 'caching_sha2_password' is not supported Python Pandas User Warning: Sorting because non-concatenation axis is not aligned

Questions with parsing tag:

Got a NumberFormatException while trying to parse a text file for objects Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) Python/Json:Expecting property name enclosed in double quotes Correctly Parsing JSON in Swift 3 How to get response as String using retrofit without using GSON or any other library in android UIButton action in table view cell "Expected BEGIN_OBJECT but was STRING at line 1 column 1" How to convert an XML file to nice pandas dataframe? How to extract multiple JSON objects from one file? How to sum digits of an integer in java? Pandas read_csv low_memory and dtype options How can I fix MySQL error #1064? Convert JSON String to JSON Object c# IsNumeric function in c# A JSONObject text must begin with '{' at 1 [character 2 line 1] with '{' error Parse json string to find and element (key / value) Retrieving values from nested JSON Object C# Parsing JSON array of objects Parsing JSON in Java without knowing JSON format Parse String date in (yyyy-MM-dd) format Jquery Smooth Scroll To DIV - Using ID value from Link Parsing JSON array into java.util.List with Gson Parsing Json rest api response in C# Parsing a pcap file in python In C#, how to check whether a string contains an integer? How can I parse a String to BigDecimal? PHP parse/syntax errors; and how to solve them PHP CSV string to array Reading input files by line using read command in shell scripting skips last line JSON Parse File Path Parse JSON file using GSON Parsing huge logfiles in Node.js - read in line-by-line Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss Splitting on last delimiter in Python string? How to avoid warning when introducing NAs by coercion Parser Error when deploy ASP.NET application Malformed String ValueError ast.literal_eval() with String representation of Tuple Loop through all elements in XML using NodeList Finding last occurrence of substring in string, replacing that Parse (split) a string in C++ using string delimiter (standard C++) JSON response parsing in Javascript to get key/value pair Extract data from XML Clob using SQL from Oracle Database Parse a URI String into Name-Value Collection Read and parse a Json File in C# ParseError: not well-formed (invalid token) using cElementTree Read .csv file in C How to install beautiful soup 4 with python 2.7 on windows Text File Parsing with Python Parsing JSON string in Java How to parse freeform street/postal address out of text, and into components

Questions with url tag:

What is the difference between URL parameters and query strings? Allow Access-Control-Allow-Origin header using HTML5 fetch API File URL "Not allowed to load local resource" in the Internet Browser Slack URL to open a channel from browser Getting absolute URLs using ASP.NET Core How do I load an HTTP URL with App Transport Security enabled in iOS 9? Adding form action in html in laravel React-router urls don't work when refreshing or writing manually URL for public Amazon S3 bucket How can I append a query parameter to an existing URL? How to get domain root url in Laravel 4? How to download image from url Laravel assets url How do I choose the URL for my Spring Boot webapp? How to read html from a url in python 3 ./xx.py: line 1: import: command not found Check if url contains string with JQuery Apache: The requested URL / was not found on this server. Apache How to get base URL in Web API controller? How to get URL parameter using jQuery or plain JavaScript? Given URL is not allowed by the Application configuration Facebook application error how to set image from url for imageView How get value from URL Javascript-Setting background image of a DIV via a function and function parameter Converting Java file:// URL to File(...) path, platform independent, including UNC paths PHP check if url parameter exists Is there a way to pass javascript variables in url? How to change the URL from "localhost" to something else, on a local system using wampserver? How do I open a URL from C++? Escaping ampersand in URL URL rewriting with PHP POST: sending a post request in a url itself Get current URL path in PHP A html space is showing as %2520 instead of %20 How do I get the different parts of a Flask request's url? Convert blob URL to normal URL Removing the fragment identifier from AngularJS urls (# symbol) Get host domain from URL? Remove all special characters from a string Submit HTML form on self page SecurityError: The operation is insecure - window.history.pushState() Passing multiple variables to another page in url Create URL from a String How to specify a local file within html using the file: scheme? how to read xml file from url using php How to launch Safari and open URL from iOS app Curl Command to Repeat URL Request CodeIgniter -> Get current URL relative to base url how to send an array in url request Optional query string parameters in ASP.NET Web API