The Wayback Machine - https://web.archive.org/web/20240118184150/https://www.geeksforgeeks.org/python-os-chdir-method/
Open In App
Related Articles

Python os.chdir() method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. 
os.chdir() method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.
 

Syntax: os.chdir(path)
Parameters: 
path: A complete path of directory to be changed to new directory path.
Returns: Doesn’t return any value

Code #1: Use chdir() to change the directory

Python3




# Python3 program to change the
# directory of file using os.chdir() method
 
# import os library
import os
 
# change the current directory
# to specified directory
os.chdir(r"C:\Users\Gfg\Desktop\geeks")
 
print("Directory changed")


Output:  

Directory changed

Code #2: Use of os.getcwd() 
To know the current working directory of the file, getcwd() method can be used. After changing the path, one can verify the path of current working directory using this method. 

Python3




# import os module
import os
 
# change the current working directory
# to specified path
os.chdir('c:\\gfg_dir')
 
# verify the path using getcwd()
cwd = os.getcwd()
 
# print the current directory
print("Current working directory is:", cwd)


Output:  

Current working directory is: c:\\gfg_dir

  
Code #3: Handling the errors while changing the directory 

Python3




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


Output: 

Inserting inside- c:\gfg_dir\gfg
Something wrong with specified directory. Exception- 
Restoring the path
Current directory is- c:\gfg_dir\gfg

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 01 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials