Python3 os.pardir Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

os.pardir() gets the parent directory (the previous level directory) of the current directory, and displays the directory name as a string.

Note:Windows and POSIX return..。

Syntax

pardir()The method syntax is as follows:

os.pardir

Parameters

  • None.

Return Value

Returns the parent directory of the current directory, with a default value of..。

Example

The following example demonstrates the use of the pardir() method:

Example

import os
 
# Output default value ..
print(os.pardir)

The output of the above program is:

..

Print the parent directory of the current directory:

Example

import os
 
 
# Current working directory
path = os.getcwd()
print("Current working directory: ", path)
 
# Parent directory
parent = os.path.join(path, os.pardir)
 
# Parent directory
print("\nParent directory:", os.path.abspath(parent))

The output of the above program is:

当前工作目录:  /Users/example/python

父目录: /Users/example

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions