Get parent directory in Python
This tutorial will explain various ways to get the parent directory of a path in Python. A parent directory is a directory above or above a given directory or file. For example, C:\folder\subfolder\myfile.txt
the parent directory of the path is C:\folder\subfolder
. Every directory, except the root directory, has a parent directory.
Get the parent directory in Python using pathlib
the module'spath.parent()
path.parent()
The method, as the name suggests, returns the parent directory of the path passed as an argument in the form of a string. Therefore, to get the parent directory of a path, we need to pass the path string to the method pathlib
of the module path.parent()
.
path.parent()
The following sample code demonstrates how to use to get the parent directory of a path in Python .
from pathlib import Path
path1 = Path(r"C:\folder\subfolder\myfile.txt")
path2 = Path(r"C:\Myfile.txt")
print(path1.parent)
print(path2.parent)
Output:
C:\folder\subfolder
C:\
Get the parent directory in Python using os
the module'spardir()
os.pardir
Is a constant string that refers to the parent directory. For Windows and POSIX operating systems, it is '..'
, and for Mac operating systems, it is '::'
.
When we combine os.path.join()
the given path with in the method os.pardir
, we can get the parent directory of the given directory.
The following example code demonstrates how to use the and methods os
of the module to get the parent directory of a path.os.pardir
path.join()
import os.path
path1 = r"C:\folder\subfolder\myfile.txt"
path2 = r"C:\Myfile.txt"
print(os.path.abspath(os.path.join(path1, os.pardir)))
print(os.path.abspath(os.path.join(path2, os.pardir)))
Output:
C:\folder\subfolder
C:\
As mentioned above, if the operating system is Windows or POSIX, we can also get the same result if we os.pardir
replace with .'..'
import os.path
path1 = r"C:\folder\subfolder\myfile.txt"
path2 = r"C:\Myfile.txt"
print(os.path.abspath(os.path.join(path1, "..")))
print(os.path.abspath(os.path.join(path2, "..")))
Output:
C:\folder\subfolder
C:\
Get the parent directory in Python using os
the module'sdirname()
os
The module's dirname()
method takes a path string as input and returns the parent directory as output.
The following sample code demonstrates how to use dirname()
the method to get the parent directory of a path.
import os.path
path1 = Path(r"C:\folder\subfolder\myfile.txt")
path2 = Path(r"C:\Myfile.txt")
print(os.path.dirname(path1))
print(os.path.dirname(path2))
Output:
C:\folder\subfolder
C:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to convert an integer to bytes
Publish Date:2025/05/08 Views:77 Category:Python
-
Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in
Catching keyboard interrupt errors in Python
Publish Date:2025/05/08 Views:106 Category:Python
-
The error occurs when a user manually tries to stop a running program using Ctrl + C or Ctrl + , or in the case of Jupyter Notebook by interrupting the kernel . To prevent accidental use of , which often occurs , we can use exception handli
Implementing a Low-Pass Filter in Python
Publish Date:2025/05/07 Views:90 Category:Python
-
Low pass filter is a term in signal processing basics and is often used to filter signals to obtain more accurate results. This tutorial will discuss the low-pass filter and how to create and implement it in Python. A low-pass filter is use
Implementing Curl command in Python using requests module
Publish Date:2025/05/07 Views:98 Category:Python
-
requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi
Using fetchall() in Python to extract elements from a database
Publish Date:2025/05/07 Views:173 Category:Python
-
This article aims to describe fetchall() the working methods of extracting elements from a database using and how to display them correctly. This article will also discuss list(cursor) how functions can be used in programs. fetchall() Extra
Parsing log files in Python
Publish Date:2025/05/07 Views:106 Category:Python
-
Log files contain information about events that occurred during the operation of a software system or application. These events include errors, requests made by users, bugs, etc. Developers can further scan these usage details to find poten
Declaring a variable without a value in Python
Publish Date:2025/05/07 Views:58 Category:Python
-
A variable is a reserved memory location that can store some value. In other words, variables in a Python program provide data to the computer to process operations. Every value in Python has a data type. There are numbers, lists, tuples, e
Defining class global variables in Python
Publish Date:2025/05/07 Views:81 Category:Python
-
A global variable is a variable that is visible and available in every part of the program. Global variables are also not defined in any function or method. On the other hand, local variables are defined in functions and can be used only in
Incrementing loop step by 2 in Python
Publish Date:2025/05/07 Views:199 Category:Python
-
In each iteration, for the loop increases the counter variable by a constant. A loop with the sequence 0, 2, 4, 6 for will increase the counter variable by 2 each iteration. This article will show you some for ways to increment by 2 in a lo