Post

PythonPython

S
Sowjanya Kodiganti

Posted on 26th April 2024|27 views

0
votes

Python Get Parent Directory

How can I get the parent directory in python?

can I get a parent directory of a path in a cross-platform method just like this

C:\Program Files ---> C:\ and C:\ ---> C:\

 

Answers
P
veerubhotla sowmithri

Posted on 26th April 2024

Try this

>>> os.path.dirname(r'C:\Program Files') 

'C:\\' 

>>> os.path.dirname('C:\\') 

'C:\\'

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos