19.4k views
2 votes
The parent_directory function returns the name of the directory that's located just above the current working directory. Remember that '..' is a relative path alias that means "go up to the parent directory". Fill in the gaps to complete this function.

import os
def parent_directory():
# Create a relative path to the parent
# of the current working directory
dir = os.getcwd()
relative_parent = os.path.join(dir, ___)
# Return the absolute path of the parent directory
return os.path.dirname(relative_parent)
print(parent_directory())

User BlueSpud
by
5.6k points

1 Answer

2 votes

Answer:

Following are the complete code to this question:

import os #import package

def parent_directory():#defining a method parent_directory

dir = os.getcwd()#defining dir that stores the getcwd method value

relative_parent = os.path.join(dir) #defining relative_parent variable that uses join method to adds directory

return os.path.dirname(relative_parent)#use return keyword to return directory name

print(parent_directory())#use print method to call parent_directory method

Output:

/

Note:

This program is run the online compiler that's why it will return "/"

Step-by-step explanation:

In the given Python code, we import the "os" package, after that a method "parent_directory" is defined, which uses the dir with the "getcwd" method that stores the current working directory.

  • After that "relative_parent" variable is declared, which uses the join method to store the directory value and use the return keyword to returns its value.
  • In the next step, the print method is used, which calls the method.
User Sandrooco
by
6.1k points