Change current working directory with Python

The OS module in Python is used for interacting with the operating system. This module comes under Python’s standard utility module so there is no need to install it externally. All functions in OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system.

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.



Syntax: os.chdir(path)

Parameters:
path: A complete path of the directory to be changed to the new directory path.

Returns: Doesn’t return any value

Example #1: We will first get the current working directory of the script and then we will change it. Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to change the
# current working directory
  
  
import os
  
# Function to Get the current 
# working directory
def current_path():
    print("Current working directory before")
    print(os.getcwd())
    print()
  
  
# Driver's code
# Printing CWD before
current_path()
  
# Changing the CWD
os.chdir('../')
  
# Printing CWD after
current_path()

chevron_right


Output:

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

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

Example #2: Handling the errors while changing the directory.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to change the
# current working directory
  
  
# importing all necessary libraries 
import sys, os 
    
# initial directory 
cwd = os.getcwd() 
    
# some non existing directory 
fd = 'false_dir/temp'
    
# trying to insert to flase directory 
try
    print("Inserting inside-", os.getcwd())
    os.chdir(fd) 
        
# Caching the exception     
except
    print("Something wrong with specified directory. Exception- ")
    print(sys.exc_info()) 
              
# handling with finally           
finally
    print()
    print("Restoring the path"
    os.chdir(cwd) 
    print("Current directory is-", os.getcwd()) 

chevron_right


Output:

Inserting inside- C:\Users\Nikhil Aggarwal\Desktop\gfg

Something wrong with specified directory. Exception-
(<class ‘FileNotFoundError’>, FileNotFoundError(2, ‘The system cannot find the path specified’), <traceback object at 0x00000268184630C8>)

Restoring the path
Current directory is- C:\Users\Nikhil Aggarwal\Desktop\gfg



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.