Handling the Current Working Directory

Consider Current Working Directory(CWD) as a folder, where Python is operating. Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be successful only if the file is in the Python’s CWD.

Note: The folder where the Python script is running is known as the Current Directory. This is not the path where the Python script is located.

Getting the Current working directory

To get the location of the current working directory os.getcwd() is used.

Example: This code uses the os' module to get and print the current working directory (CWD) of the Python script. It retrieves the CWD using the os.getcwd()' and then prints it to the console.

Python
import os 
cwd = os.getcwd() 
print("Current working directory:", cwd) 

Output:

Current working directory: /home/nikhil/Desktop/gfg

Changing the Current working directory

To change the current working directory(CWD) os.chdir() method is used. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.

Note: The current working directory is the folder in which the Python script is operating.

Example: The code checks and displays the current working directory (CWD) twice: before and after changing the directory up one level using os.chdir('../'). It provides a simple example of how to work with the current working directory in Python.

Python
import os 
def current_path(): 
    print("Current working directory before") 
    print(os.getcwd()) 
    print() 
current_path() 
os.chdir('../') 
current_path() 

Output:

Current working directory before
C:\Users\Nikhil Aggarwal\Desktop\gfg
Current working directory after
C:\Users\Nikhil Aggarwal\Desktop

OS Module in Python with Examples

The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality.

The *os* and *os.path* modules include many functions to interact with the file system.

Similar Reads

Python-OS-Module Functions

Here we will discuss some important functions of the Python os module :...

Handling the Current Working Directory

Consider Current Working Directory(CWD) as a folder, where Python is operating. Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be successful only if the file is in the Python’s CWD....

Creating a Directory

There are different methods available in the OS module for creating a directory. These are –...

Listing out Files and Directories with Python

There is os.listdir() method in Python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then the list of files and directories in the current working directory will be returned....

Deleting Directory or Files using Python

OS module provides different methods for removing directories and files in Python. These are –...

Commonly Used Functions

Using os.name function...