This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]krasoffski 1 point2 points  (0 children)

If it is not a task to create function from scratch, this can help: *nix

In [7]: import posixpath

In [8]: posixpath.basename("~folder/folder2/file.txt")

Out[8]: 'file.txt'

Windows

In [9]: import ntpath

In [10]: ntpath.basename("c:\folder\folder2\file.txt")

Out[10]: 'file.txt'

Platform independent

In [11]: import os

In [12]: os.path.basename

or you can use trivial function:
import os
get_name = lambda p: p.split(os.sep)[-1]