How to extract the last folder and file name from file path in python

Viewed 63

I have some filepaths like this:

G:\folder1\folder2\folder3\folder4\folder5\fold2/197320-6-10-0.wav

How do I extract the last folder and file name like this:

fold2/197320-6-10-0.wav

1 Answers

Possible solution is to use split() function.

text_string = r'''G:\folder1\folder2\folder3\folder4\folder5\fold2/197320-6-10-0.wav'''

data = text_string.split("\\")[-1]

print(data)

Prints

fold2/197320-6-10-0.wav
Related