How to split path and file name in Python

Suppose you have a path, for example

 
/host/name/public_html/lib/general_functions.php
 

Now you want to split the path and the file name, you want to split it into the following two parts

 
/host/name/public_html/lib/
general_functions.php
 

The idiomatic way to do this is using os.path.split method

 
>>> import os
>>> path,filename=os.path.split("/host/name/public_html/lib/general_functions.php")
>>> path
'/host/name/public_html/lib'
>>> filename
'general_functions.php'
>>>
 

The os.path.split is smart enough to accept both unix style path and windows style:

 
>>> path,filename=os.path.split("c:\\host\\name\\public_html\\lib\\general_functions.php")
>>> path
'c:\\host\\name\\public_html\\lib'
>>> filename
'general_functions.php'