How to Change the Directory in Python

To change the current working directory in Python, use the os.chdir() method. The os.chdir() is a built-in Python function used to change the current working directory to the specified path. It takes only a single argument as a new directory path.

To work with directories and files in Python, use the built-in os module. The os stands for the Operating System. The OS  module provides all the necessary functions to work with os related tasks.

The OS module is used for making the python compiler interact with the operating system. The directory is also meant as the folder. This directory consists of Path where the file is presently followed by the file name.

To get the current working directory in Python, use the getcwd() function.

import os

directory = os.getcwd()
print("The current working directory of the file is : ", directory)

Output

The current working directory of the file is : /Users/krunallathiya/Desktop/Code/R

In this program, we imported the os module. The os module has a function called os.getcwd() function that returns the current working directory.

Hence, the location in which the python file is running will be displayed as output. We can get the current working directory using this function.

Python program to change the current working directory

import os

directory = os.getcwd()
print("The current working directory of the file is : ", directory)
os.chdir("/Users/krunallathiya/Desktop")

Output

The current working directory of the file is : /Users/krunallathiya/Desktop/Code/R

In this program, we printed the current working directory. It will display the working directory of the file. Then, we changed the current working directory using the os.chdir() function.

The os.chdir() function changes the current working directory. The path which is passed inside the argument is made as to the current working directory.

To check the current directory after changing the directory, use the following code.

import os

directory = os.getcwd()
print("The current working directory of the file is : ", directory)
os.chdir("/Users/krunallathiya/Desktop")
changedDirectory = os.getcwd()
print(" The current working directory After changing the directory is : ", changedDirectory)

Output

The current working directory of the file is : /Users/krunallathiya/Desktop/Code/R
The current working directory After changing the directory is : /Users/krunallathiya/Desktop

Hence, we printed the current working directory in the beginning then we changed the directory using the chdir() function. Then we printed the current working directory after changing it. Now, the changedDirectory will be displayed as the current working directory.

Program for changing current directory with try and except block

import os

directory = os.getcwd()
print("The current working directory of the file is : ", directory)
try:
   os.chdir("/Users/krunallathiya/Desktop")
except:
   print("The directory cannot be changed")
else:
   changed = os.getcsd()
print("The current working directory After changing the directory is : ", changed)

Output

The current working directory of the file is : /Users/krunallathiya/Desktop/Code/R
The current working directory After changing the directory is : /Users/krunallathiya/Desktop

In this program, we used to try-except block for error handling. If the directory is changed then the new directory will be displayed in the new directory. Then else a message will be printed stating that the directory cannot be changed.

That’s it for this tutorial.

Related posts

How to Import from Parent Directory in Python

How to list files in directory in Python

How to Write Bytes to File in Python

Leave a Comment