How to Get and Change the Current Working Directory in Python?

Python contains an inbuilt “os” module, which allows interaction with the operating system. This module has functions that perform various functionalities, such as removing files or directories, getting the current working directories, changing directories, etc. To get and change the present working directory in Python, the inbuilt functions named os.getcwd() and os.chdir() are used.

This write-up will give you a detailed understanding of how to get and change the current working directory in Python using the following aspects:

So let’s get started!

How to Get and Change the Python Current Working Directory?

To get the Python present working directory, the “os.getcwd()” function is used. To change the present Python working directory, the “os.chdir()” is used in Python. The current working directory in Python means the directory in which our Python scripts are executed.

Using os.getcwd() Method

Top 10 Linux Distros

In the example below, the “os.getcwd()” returns the present working directory as a string value. The string retrieved from the “os.getcwd()” does not contain a trailing slash.

Code:

import os
# using os.getcwd() to get the current working directory
direct = os.getcwd()

print("Current working directory: ", direct)
print(type(direct))

In the above code:

  • The “os” module is imported.
  • The “os.getcwd()” is used to get the current working directory of Python.
  • The “os.getcwd()” returns the string value, which shows the complete path of the present working directory without the trailing slash.

Output:

The above output shows the current working directory along with the return data type.

Using os.chdir() Method

In the example given below, the present working directory will be changed to a new directory using the “os.chdir()” function.

Code:

import os

current_dirct = os.getcwd()
print('Current Working Directory: ', current_dirct)

# Change the working directory
os.chdir('F:\itslinuxfoss\Sample')

dirct = os.getcwd()
print('New Current Working Directory: ', dirct)

In the above code:

  • The “os” module is imported at the start of the program for accessing “os.getcwd()” and “os.chdir()” functions.
  • First, the “os.getcwd()” function gets the Python program’s current working directory.
  • To change the Python working directory, the new directory “path” is passed inside the parentheses of “os.chdir()” function.
  • Lastly, the current working directory is checked again using the “os.getcwd()” function.

Note: The function “os.chdir()” can return errors like “FileNotFoundError”, “PermissionError” and “NotADirectoryError”. These errors occur if the specified directory does not exist or the script does not have permission.

Output:

The above output verified that the current directory had been changed to a new directory.

That’s it from this blog!

Conclusion

The “os.getcwd()” function is used to get the present Python working directory. Similarly, the “os.chdir()” function changes the current Python working directory. Using “os.getcwd(),” you can retrieve the main path to the current working directory without the trailing slash. The “os.chdir()” accepts the path as a parameter value and changes the working directory to the new specified directory path. This Python blog post demonstrated how to get the working directory and change it in Python via practical examples.