-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?

2
  • 1
    Why is the "code not shown", please show us the code Commented Jun 15, 2019 at 21:57
  • 1
    Are you looking for the os.path.basename function? Commented Jun 15, 2019 at 21:58

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.