Skip to content

How to get the current working directory in Python?

One of the most important aspects you should take care of when programming in just about any language is aware of your project directory. 

When working in Python, it’s always a good idea to use absolute paths. You’ll need to know about the concept of changing or accessing the current working directory only when you’re working with relative paths. 

By default, when you run a python script, the current working directory is where you’re executing the script. 

In this article, we’re taking a look at how to fetch and change the current working directory in Python. 

Also read: How to concatenate strings in Python?


How to fetch the current working directory in Python?

The os module in Python has a bunch of methods that come in handy when working with directories. The getcwd() method from the os module can fetch the path of your current working directory. 

Do keep in mind that you’re going to have to import the os `module to be able to use this method.  The method outputs the path as a string without the trailing slash.

import os
cwd = os.getcwd()
print("Current working directory: {0}".format(cwd))

The above snippet will output the path of the current working directory you’re script is running in currently. 

Also read: How to concatenate strings in Python?


How to change the current working directory in Python?

If you need to change your current working directory for whatever reason, we’re going to need another method from the os module — the chdir() method. 

The method takes the destination path as an argument which can be either relative or absolute.

import os
print("Current working directory: {0}".format(os.getcwd()))
os.chdir('/tmp')
print("Current working directory: {0}".format(os.getcwd()))

The above snippet will first print your current working directory, then change it to the /tmp directory and print the path again. 

Here are some of the exceptions that you can run into if you misuse the method.

  • NotADirectoryError: The argument provided to chdir() should be a directory only. If not, you’ll receive this exception.
  • FireNotFoundError : If the target directory doesn’t exist, a FireNotFoundError exception is raised.
  • PermissionError : This exception is raised when the script doesn’t have the required permissions to access the target directory.

Also read: How to run Python in terminal?


Other helpful methods from the os module

Aforementioned, the os module in Python has a bunch of useful methods that can be used for several different operations. Here are a few of the commonly used ones.

  • os.listdir(): Lists out all files and sub-folders in a directory.
  • os.mkdir(‘dirName’): Creates a new directory with the supplied name.
  • os.rename(‘oldName’, ‘newName’): Renames any file or folder inside the current active directory with the given name and changes it to the new name provided. 
  • os.remove(*fileName’): Removes the specified file from the active directory. 
  • os.rmdir(‘dirName’): Removes the specified empty folder from the active directory. 
  • shutil.retree(‘dirName’): Deletes the specified non-empty folder from the active directory. Keep in mind that you’ll have to import the shutil library before using this method. 

Also read: How to split Python strings?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. If you're running an Adblocker, we humbly request you to whitelist us.

Share on facebook
Share on whatsapp
Share on twitter
Share on reddit
Share on linkedin
Share on pocket
Share on pinterest
Share on telegram
Share on stumbleupon
Share on digg
Share on tumblr
Share on email
Share on skype
Share on xing
Share on vk
Share on odnoklassniki
Share on mix








>