pythonpathsubdirectory

In python how do you get the "last" directory in a path string?


I am working on a remote file system, where I don't have direct access to the files/directories, so I cannot check if a string represents a file or a directory.

I have the following paths I need to handle, where I have to get a hold of the "partition column":

path1 = "/path/to/2012-01-01/files/2014-01-31/la.parquet"
path2 = "/path/to/2012-01-01/files/2014-01-31/"
path3 = "/path/to/2012-01-01/files/2014-01-31"

In all cases, the deepest path (partition column) is "2014-01-31". Is there a way consistently to get this path in a single line of code, or do I have to do all sorts of checks of file names?

I was hoping to do something like:

import os
os.path.dirname(path).split("/")[-1]

But this doesn't work for path3. Does one need to have access to the filesystem to correctly identify the deepest directory, or is there some easy way?


Solution

  • This can be solved using pathlib. Here is a straightforward solution for the same.

    import pathlib
    
    
    path1 = pathlib.Path("/path/to/2012-01-01/files/2014-01-31/la.parquet")
    print(path1.parent.name if not path1.is_dir() else path1.name)
    

    [EDIT]:- Handling cases when the last path can be a directory or a file

    Reference