|
| Home |
|---|
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.
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.
import os
The basic syntax for changing the current working directory in Python is:
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.
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.
import os
os.chdir("C:/Users")
print(os.getcwd())
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:
os.chdir() changes the current working directory.os.getcwd() returns the current working directory.import os loads Python's operating system module.
os.chdir() changes the current working directory to the
specified folder.
os.getcwd() returns the current working directory so that
you can verify the change.
Using os.getcwd() after os.chdir() is a simple
way to confirm that Python has switched to the expected directory.
A common use of os.chdir() is switching to a project folder
before working with files stored in that folder.
import os
os.chdir("C:/Users/Student/PythonProjects")
print("New working directory:")
print(os.getcwd())
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.
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.
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.
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.
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.
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.
Instead of writing the full directory path, Python also allows you to
use relative paths with os.chdir().
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.
You can also use .. in a relative path to move to the parent
directory.
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.
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"
Beginners commonly encounter errors when changing the current working directory. The most frequent problems include:
os module.
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().
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:
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.
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.
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.
Continue learning Python with these related tutorials:
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.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net