Dealing with paths and filenames in python3

This is a quick reference for some of the common operations on filenames and paths in python3

Extract filename from path

        
import os
base=os.path.basename('/path/to/file.ext')
# base = 'file.ext'
      
  

Extract filename from path without extension

        

import os
base_without_extension=os.path.splitext(os.path.basename('/path/to/file.ext'))[0]
# base_without_extension = 'file'
        
    

Extract full path to directory containing a file

        

import os
directory_path = os.path.dirname('/path/to/file.ext')
# directory_path = '/path/to'