Given a path to a file, c:\\xxx\\abc\\xyz\\fileName.jpg
, how can I get the file\'s parent folder? In this example, I\'m looking for xyz
. The number
Use os.path.dirname to get the directory path. If you only want the name of the directory, you can use os.path.basename to extract the base name from it:
>>> path = r'c:\xxx\abc\xyz\fileName.jpg'
>>> import os
>>> os.path.dirname(path)
'c:\\xxx\\abc\\xyz'
>>> os.path.basename(os.path.dirname(path))
'xyz'