How to split a path into its components in Python

Splitting a path into its components separates it into a list of strings. For example, splitting the path “/home/user/pictures/kite.jpg” into its components returns [“”, “home”, “user”, “pictures”, “kite.jpg”].

Solution for How to split a path into its components in Python : You can use os.path.normpath() and str.split() to split a path into its components Call os.path.normpath(path) to return a normalized string from path. Use str.split(sep) with the resultant string as str and os.sep as sep to return a list of the path components.


how-to-split-a-path-into-its-components-in-python