Python Tutoring Online
 Home            

Python os.chdir() – Change Current Working Directory

When working with files and folders in Python, a program operates inside a folder called the current working directory. The current working directory is the location Python uses when working with relative file paths.

Python provides the os.chdir() function to change the current working directory to another folder. The name chdir stands for change directory.

If you are searching for Python setcwd() or wondering how to set the current working directory in Python, the correct function to use is os.chdir(). Python does not provide a built-in setcwd() function. Instead, the os.chdir() function is used to change the current working directory.

The chdir() function belongs to Python's built-in os module, which provides functions for interacting with the operating system, files and directories.

In this tutorial, you will learn how to use os.chdir() with practical examples, how to check the current directory with os.getcwd(), how to use relative paths, and how changing the working directory affects file operations in Python.

Importing the os Module

Before using os.chdir(), the os module must be imported. The module is part of Python's standard library, so you do not need to install any additional package.

Python
import os

Python os.chdir() Syntax

The basic syntax for changing the current working directory in Python is:

Python
os.chdir(path)

The path parameter specifies the directory that Python should switch to. The path can be an absolute path, such as C:/Users/Student/Documents, or a relative path, such as data.

The directory specified by the path must exist. If Python cannot find the specified directory, os.chdir() raises a FileNotFoundError.

How to Change the Current Working Directory in Python

You can change the current working directory by passing a directory path to os.chdir(). After changing the directory, you can use os.getcwd() to check the new location.

Python
import os

os.chdir("C:/Users")

print(os.getcwd())

Output


C:\Users

The program changes the current working directory to C:/Users. The os.getcwd() function then returns the new current working directory.

This is a useful pattern to remember:

Understanding the Code

Using os.getcwd() after os.chdir() is a simple way to confirm that Python has switched to the expected directory.

Example: Switching to a Project Folder

A common use of os.chdir() is switching to a project folder before working with files stored in that folder.

Python
import os

os.chdir("C:/Users/Student/PythonProjects")

print("New working directory:")
print(os.getcwd())

Output


New working directory:
C:\Users\Student\PythonProjects

In this example, Python changes the current working directory to the PythonProjects folder and then displays the new location.

Using os.chdir() Before Opening Files

Changing the current working directory can be useful when a Python program needs to work with files located in a particular folder.

For example, suppose data.txt is stored inside the Documents folder. The program can change to that folder and then open the file using its relative filename.

Python
import os

os.chdir("C:/Users/Student/Documents")

file = open("data.txt")

print(file.read())

The program first switches to the Documents folder. Because data.txt is then specified using a relative path, Python looks for the file in the new current working directory.

This is one reason understanding the current working directory is important when learning Python file handling.

Python getcwd() vs chdir()

The functions getcwd() and chdir() are closely related, but they perform different tasks. The os.getcwd() function tells you where the program currently is, while os.chdir() changes that location.

Function Purpose
os.getcwd() Returns the current working directory
os.chdir() Changes the current working directory

You can learn more about finding the current directory in our Python getcwd() tutorial.

Example

Python
import os

print("Current directory:")
print(os.getcwd())

os.chdir("C:/Users")

print("New directory:")
print(os.getcwd())

In this example, os.getcwd() is used before and after os.chdir() so that the change in the working directory can be clearly seen.

Understanding Absolute and Relative Paths

The path supplied to os.chdir() can be either an absolute path or a relative path. Understanding the difference is important when working with Python directories and files.

An absolute path gives the complete location of a directory. For example, on Windows an absolute path might look like:


C:\Users\Student\Documents

A relative path describes a location in relation to the current working directory. For example:


data

When you use a relative path with os.chdir(), Python interprets that path relative to the current working directory.

Using Relative Paths with os.chdir()

Instead of writing the full directory path, Python also allows you to use relative paths with os.chdir().

Python
import os

os.chdir("data")

print(os.getcwd())

In this example, Python switches to a folder named data inside the current working directory.

For example, if the current working directory is C:/Users/Student/PythonProjects, the relative path data refers to:


C:\Users\Student\PythonProjects\data

Relative paths are convenient because they make programs easier to move between computers and project folders. However, you need to make sure that the expected folder exists relative to the current directory.

Changing to a Parent Directory

You can also use .. in a relative path to move to the parent directory.

Python
import os

os.chdir("..")

print(os.getcwd())

The .. notation refers to the parent directory of the current working directory. This can be useful when navigating between related project folders.

Windows, macOS and Linux Paths

The path format depends on the operating system. On Windows, paths commonly contain a drive letter such as C:, while macOS and Linux use paths beginning with /.

A Windows path can be written as:


C:\Users\Student\Documents

A macOS or Linux path can look like:


/home/student/Documents

Python also accepts forward slashes in Windows paths, which can make paths easier to read in Python code:


C:/Users/Student/Documents

When working with Windows paths, raw strings can also be useful when a path contains backslashes.


r"C:\Users\Student\Documents"

Common Beginner Errors with os.chdir()

Beginners commonly encounter errors when changing the current working directory. The most frequent problems include:

What Happens When os.chdir() Fails?

If the directory specified in os.chdir() does not exist, Python raises an error rather than changing the working directory. For example, attempting to switch to a nonexistent folder can produce a FileNotFoundError.

You should therefore check that the directory exists before changing to it when your program receives a path from a user or another external source.

The os.path.exists() function can be used to check whether a path exists before calling os.chdir().

Important: os.chdir() Changes the Working Directory

When you call os.chdir(), the current working directory of the running Python process changes. This means that relative file paths used later in the program are interpreted relative to the new directory.

For example, after running:

Python
import os

os.chdir("C:/Users/Student/Documents")

print(os.getcwd())

a relative filename such as data.txt will be looked for in the new Documents directory rather than the directory that was previously the current working directory.

This is important to remember when a program works with multiple folders or when different parts of a program use relative file paths.

When Should You Use os.chdir()?

The os.chdir() function can be useful in scripts that need to work with files in a specific directory. It may be used in file processing, automation scripts, data analysis projects and other programs that interact with the operating system.

However, changing the working directory is not always necessary. For many applications, it can be clearer to work with explicit file paths instead of repeatedly changing the current directory.

For beginners, the most important concept is to understand how the current working directory affects relative paths and file operations.

Summary

Python uses the current working directory when working with relative file and directory paths. The os.chdir() function allows a program to change this directory.

The basic syntax is os.chdir(path), where path specifies the directory to which Python should switch. You can use os.getcwd() to check the current working directory before or after making the change.

Remember that Python does not have a built-in setcwd() function. If you are looking for Python setcwd(), the correct function for changing the current working directory is os.chdir().

Understanding os.chdir(), os.getcwd(), absolute paths and relative paths is an important foundation for learning Python file handling and working with the operating system.

Related Python Tutorials

Continue learning Python with these related tutorials:

Need Help Learning Python?

Having difficulty understanding Python directories, file paths, os.getcwd() or os.chdir()? An experienced Python tutor can help you understand these concepts step by step.

One-to-one Python tutoring sessions provide clear explanations, practical coding practice and help with Python assignments and projects. Sessions can be tailored to your current level and learning goals.

Get Python Tutoring Help




Stuck Learning Python?

Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.

Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net