+

Python | os.getcwd () method

The

OS Module in Python provides functions for interacting with the operating system. The OS comes with standard Python service modules. This module provides a portable way to use operating system specific functionality. 
All functions in the os module raise OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system. 
os.getcwd () tells us the location of the current working directory (CWD).

Syntax: os.getcwd ()

Parameter: No parameter is required.

Return Value: This method returns a string which represents the current working directory.

Example # 1:

Using os.getcwd () to get the current working directory

# Python program to explain the os.getcwd () method

 
# import of the os module

import os 

 
# Get current work
# directory (CWD)

cwd = os.getcwd () 

 
# Print current work
# directory (CWD)

print ( "Current working directory:" , cwd) 

Exit:

 Current working directory: C: UsersRajnishAppDataLocalProgramsPythonPython37 

Example # 2:
Using os.getcwd () to get the current Python.Engineering working directory, which is root

# Python program to explain the os.getcwd () method

 
  # import of the os module

import os 

 
# Get current work
# catalog (CWD)

cwd = os.getcwd () 

 
# Print current job
# directory (CWD)
# which in this case is root

print ( "Current working directory:" , cwd) 

Exit:

 Current working directory: / 
Get Solution for free from DataCamp guru