Fix Python – What is the difference between os.path.basename() and os.path.dirname()?

Question

Asked By – user1429210

What is the difference between os.path.basename() and os.path.dirname()?

I already searched for answers and read some links, but didn’t understand.
Can anyone give a simple explanation?

Now we will see solution for issue: What is the difference between os.path.basename() and os.path.dirname()?


Answer

Both functions use the os.path.split(path) function to split the pathname path into a pair; (head, tail).

The os.path.dirname(path) function returns the head of the path.

E.g.: The dirname of '/foo/bar/item' is '/foo/bar'.

The os.path.basename(path) function returns the tail of the path.

E.g.: The basename of '/foo/bar/item' returns 'item'

From: http://docs.python.org/3/library/os.path.html#os.path.basename

This question is answered By – Breno Teixeira

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0