I have a string containing a filename and optionally can be a full path or a relative one.
os.path module seems to miss such function. What is the easiest solution?
I have a string containing a filename and optionally can be a full path or a relative one.
os.path module seems to miss such function. What is the easiest solution?
Method 1 : ( way to Go )
from os.path import basename
filename = basename("/home/user/file.txt")
Method 2 : ( Seems good but not a good method )
mypath = "/home/user/file.txt"
filename = mypath[mypath.rfind("/")+1:]
Method1 works in all cases, where as method2 will break often specially when moving platforms.
That is why we use os, that changes the underline logic with changing platforms, this is how python provides platform independence - Keeping the logic independent of os details.
basename neglects all the path information. This is useful only if you're trying to actually get the basename and not the Path.