find the name of a directory in python

You can find the name of a directory in Python using the os module or the pathlib module. Both of these modules have functions that allow you to get information about directories.

Here is an example using os:

main.py
import os

path = "/path/to/directory"
dir_name = os.path.basename(path)

print(dir_name)
90 chars
7 lines

This will print the name of the directory at the given path.

Here is an example using pathlib:

main.py
from pathlib import Path

path = Path("/path/to/directory")
dir_name = path.name

print(dir_name)
98 chars
7 lines

This will also print the name of the directory at the given path.

gistlibby LogSnag