logo-MindMajix
PostPythonPython
S
Sowjanya Kodiganti

Posted on 25th July 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:\

 

1 answers
Answers
P
veerubhotla sowmithri

Posted on 25th July 2024| views

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
Name
Course *
Email *
Phone Number