Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
How to get parent folder name of current directory?

I know there are functions for finding parent directory or path such as.

os.path.dirname(os.path.realpath(__file__))

'C:\Users\jahon\Desktop\Projects\CAA\Result\caa\project_folder'

Is there a function that just returns the parent folder name? In this case it should be project_folder.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can achieve this easily with os

import os
os.path.basename(os.getcwd())
over 4 years ago · Santiago Trujillo Denunciar

0

You can get the last part of any path using basename (from os.path):

>>> from os.path import basename
>>> basename('/path/to/directory')
'directory'

Just to note, if your path ends with / then the last part of the path is empty:

>>> basename('/path/to/directory/')
''
over 4 years ago · Santiago Trujillo Denunciar

0

Yes, you can use PurePath.

PurePath(__file__).parent.name == 'parent_dir'
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda