Site icon DesignLinux

Get and Change the Current Working Directory in Python

python-get-change-current-working-directory

In Python, it’s a best practice to use the absolute paths. It’s little bit difficult while you are working with the relative paths. An absolute path specifies a file or directory location starting from the root directory, while the relative path begins from the current working directory. At the time of execution of Python script, you current working directory is set the directory from where you run the script.

Usign os python module, it can interact with the operating system. Python includes this module in the standard python library and way for finding and changing the current working directory.

Getting the Current Working Directory in Python

Use the getcwd() method of the os module in Python, which will return a string containing the absolute path of the current working directory. The returned string does not include the trailing slash character.

os.getcwd()

You must import the module at the top of the file to use the os module method.

Following is the example to display the current working directory:

# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))

It will show the output as following:

Current working directory: /home/linuxize/Desktop
os.getcwd() returns an object of type: <class 'str'>

Change the Current Working Directory in Python

Use the chdir() method to change the current working directory in Python.

os.getcwd(path)

You should pass the path of the directory where you want to change as an argument of this method. The path argument can be absolute or relative.

Below is an example:

# Import the os module
import os

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

# Change the current working directory
os.chdir('/tmp')

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

The output will look something like this:

Current working directory: /home/tecnstuff/Desktop
Current working directory: /tmp

Make sure that the argument passed in to the chdir() method must be a directory, otherwise it will throw and NotADirectoryError exception. If the directory doesn’t exist, a FileNotFoundError exception is raised. It will show the PermissionError exception if the user doesn’t have required permissions.

# Import the os module
import os

path = '/var/www'

try:
    os.chdir(path)
    print("Current working directory: {0}".format(os.getcwd()))
except FileNotFoundError:
    print("Directory: {0} does not exist".format(path))
except NotADirectoryError:
    print("{0} is not a directory".format(path))
except PermissionError:
    print("You do not have permissions to change to {0}".format(path))

Conclusion

You have learned how to find the current working directory in Python and how to change the current working directory in Linux.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version