I have a given file path. For example, "C:\Users\cobyk\Downloads\GrassyPath.jpg". I would like to pull in a separate string, the image file name.
I'm assuming the best way to do that is to start from the back end of the string, find the final slash and then take the characters following that slash. Is there a method to do this already or will I have search through the string via a for loop, find the last slash myself, and then do the transferring manually?
os.path.basename()to get the filename?os.path.basename(s)is designed for that task.os.pathis sooo ancient, for new code I highly recommend using thepathlibmodule that was added in Python 3.4 (which was released 2014-03-16).