Skip to content
Advertisement

Extract a part of the filepath (a directory) in Python

I need to extract the name of the parent directory of a certain path. This is what it looks like:

JavaScript

I would like to extract directory_i_need.

Advertisement

Answer

JavaScript

And you can continue doing this as many times as necessary…

Edit: from os.path, you can use either os.path.split or os.path.basename:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement