- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I get the parent directory in Python?
In Python 3.4+ you can use the pathlib module to get the parent directory.
Example
from pathlib import Path print(Path('/home/username').parent)
Output
This will give the output:
/home
In older versions, you can call the os.path.join on your path and '..'(represents parent directory) and then find its absolute path using os.path.abspath.
Example
import os print(os.path.abspath(os.path.join('/home/username', '..')))
Output
This will give the output:
/home
- Related Articles
- Java Program to get name of parent directory
- Java Program to get the name of the parent directory of the file or directory
- How do I list all files of a directory in Python?
- How do I get a parent HTML Tag with Selenium WebDriver using Java?
- How do I find the location of my Python site-packages directory?
- How to get the home directory in Python?
- How do you get a directory listing sorted by creation date in Python?
- How do you get a directory listing sorted by their name in Python?
- How do I get rid of the Python Tkinter root window?
- How do I get current URL in Selenium Webdriver 2 Python?
- How do I get list of methods in a Python class?
- How do I get the id after INSERT into MySQL database in Python?
- How can I list the contents of a directory in Python?
- How do I get IntelliJ to recognize common Python modules?
- How do I get a job as a Python developer?

Advertisements