Python - os.pardir() method with example Last Updated : 18 May, 2020 Comments Improve Suggest changes Like Article Like Report In Python, OS module provides various functions to interact with the operating system. This module comes under the Python standard utility module, so there is no need to install it manually. os.pardir is a constant string used by the operating system to refer to the parent directory. This method is also available via os.path.pardir() Note: os.pardir is '.. for UNIX based OS and '::' for Mac OS. Syntax: os.pardir Return type: a string that refers to the parent directory. Example 1: Python3 1== # Python program to demonstrate # os.pardir import os # prints .. by default print(os.pardir) Output: .. Example 2: Let's print the parent of current working directory. Python3 1== # Python program to demonstrate # os.pardir import os # current working directory path = os.getcwd() print("Current Directory:", path) # parent directory parent = os.path.join(path, os.pardir) # prints parent directory print("\nParent Directory:", os.path.abspath(parent)) Output: Current Directory: /home/geeks/Desktop/gfg Parent Directory: /home/geeks/Desktop Example 3: Getting the parent of specified path. Python3 1== # Python program to demonstrate # os.pardir import os # path path = "your/path/for/parent/directory" print("Path:", path) # parent parent = os.path.join(path, os.pardir) # prints the relative file path # for the current directory (parent) print("\nParent:", os.path.relpath(parent)) Output: Path: your/path/for/parent/directory Parent: your/path/for/parent Comment More infoAdvertise with us Next Article Python - os.pardir() method with example N nikhilaggarwal3 Follow Improve Article Tags : Python Python Programs python-os-module Python directory-program Practice Tags : python Similar Reads os.path.abspath() method - Python The os.path.abspath() method in Python's os module is used to get the full (absolute) path of a file or folder. It's useful when you're working with relative paths but need to know the exact location on your system. If you pass a relative path, it converts it to an absolute one and if the path is al 2 min read Create a File Path with Variables in Python The task is to create a file path using variables in Python. Different methods we can use are string concatenation and os.path.join(), both of which allow us to build file paths dynamically and ensure compatibility across different platforms. For example, if you have a folder named Documents and a f 3 min read How to use OS with Python List Comprehension One such module is the 'os' module, which allows interaction with the operating system. When combined with list comprehensions, the 'os' module becomes a potent tool for handling file and directory operations concisely and expressively. In this article, we will see how we can use the os module with 3 min read Python Program to Safely Create a Nested Directory Creating a nested directory in Python involves ensuring that the directory is created safely, without overwriting existing directories or causing errors. To create a nested directory we can use methods like os.makedirs(), os.path.exists(), and Path.mkdir(). In this article, we will study how to safe 2 min read Delete a directory or file using Python In this article, we will cover how to delete (remove) files and directories in Python. Python provides different methods and functions for removing files and directories. One can remove the file according to their need.Table of ContentUsing the os.remove() MethodDelete a FileRemove file with absolut 6 min read Python Program to Get the File Name From the File Path In this article, we will be looking at the program to get the file name from the given file path in the Python programming language. Sometimes during automation, we might need the file name extracted from the file path. Better to have knowledge of:Python OS-modulePython path moduleRegular expression 5 min read How to Use Regex with os.listdir() in Python? We are given a file path and our task is to find out the usage of regex with os.listdir() in Python by using that path and files inside that directory. In this article, we will see the usage of Regex with os.listdir() in Python with code examples. Regex with os.listdir() in PythonIn Python, the os.l 3 min read Python | Sort and store files with same extension Have you ever wanted to find any particular file in a folder, but then completely freak out when you find that folder to be a hell of a mess? Well, Python is a rescue here. Using Python OS-module and shutil module, we can organize the files with same extensions and store in separate folders. Look at 2 min read Python Loop through Folders and Files in Directory File iteration is a crucial process of working with files in Python. The process of accessing and processing each item in any collection is called File iteration in Python, which involves looping through a folder and perform operation on each file. In this article, we will see how we can iterate ove 4 min read File System Manipulation in Python File system manipulation in Python refers to the ability to perform various operations on files, such as creating, reading, writing, appending, renaming, and deleting. Python provides several built-in modules and functions that allow you to perform various file system operations. Python treats files 3 min read Like