-1

I am passing a file path to a method. An example is the following: The method--

def example(image):
     code not shown

The call to the method--

example("resources/1.png")

In this, I want to remove the "resources/" part using the example method and simply return "1.png". How can I do that?

1 Answers1

3

The standard library has exactly the function you're looking for, it's called basename. you just need to import os:

>>> import os                                                                                                                                                                                                                                                  
>>> os.path.basename("resources/1.png")                                                                                                                                                                                                                        
'1.png'

I guess your function could then be written as:

from os import path  # Usually goes at the top of the file

def example(image):
    return path.basename(image)
André Laszlo
  • 15,169
  • 3
  • 63
  • 81