Site icon DesignLinux

How to Get and Change the Current Working Directory in Python

When dealing with files in directories in Python, it is always a good idea to use absolute paths. However, if you are working with relative paths you’ll need to understand the concept of current working directory and how to find or change the current working directory. An absolute path specifies a file or directory location starting from the root directory, while the relative path begins from the current working directory.

When you run a Python script, the current working directory is set to the directory from which the script is executed.

The os python module provides a portable way to interact with the operating system. The module is part of the standard Python library and includes methods for finding and changing the current working directory.

Getting the Current Working Directory in Python

The getcwd() method of the os module in Python, returns a string that contains the absolute path of the current working directory. The returned string does not include the trailing slash character.

os.getcwd()

To use the os module methods, you must import the module at the top of the file.

Below is an example showing how to print 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)))

The output will look something like this:

Current working directory: /home/linuxize/Desktop
os.getcwd() returns an object of type: <class 'str'>
If you want to find the directory where the script is located, use os.path.realpath(__file__). It will return a string containing the absolute path to the running script.

Changing the Current Working Directory in Python

To change the current working directory in Python, use the chdir() method.

os.getcwd(path)

The method accepts one argument, the path to the directory to which you want to change. The path argument can be absolute or relative.

Here 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/linuxize/Desktop
Current working directory: /tmp

The argument provided to the chdir() method must be a directory, otherwise NotADirectoryError exception is raised. If the specified directory doesn’t exist, a FileNotFoundError exception is raised. If the user under which the script is running doesn’t have the necessary permissions, a PermissionError exception is raised.

# 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

To find the current working directory in Python, use os.getcwd(), and to change the current working directory, use os.chdir(path).

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

python
Exit mobile version