os.chdir() Function Explained
os.chdir() changes Python’s current working directory.
This matters because the current working directory is the folder Python uses when you give a relative path like "data.txt" or "images/photo.jpg". After you change directories, file operations such as open() will use the new folder.
If you are learning how files and folders work in Python, os.chdir() can be useful. But it can also cause confusion if you are not sure which folder your program is currently using.
Quick example
import os
print(os.getcwd())
os.chdir("my_folder")
print(os.getcwd())
Use os.getcwd() before and after os.chdir() to confirm that the working directory changed.
What os.chdir() does
os.chdir() changes the program's current working directory.
In simple terms:
- The working directory is the folder Python is currently “inside”
- Relative file paths are based on that folder
- After changing it, code like
open("notes.txt")looks in the new folder
For example, if your current folder is:
C:\Users\Sam\project
and you run:
open("data.txt")
Python looks for data.txt inside that folder.
If you then use os.chdir("reports"), Python will start looking relative to the reports folder instead.
If you are new to this idea, the os module overview and os.getcwd() function explained pages are good next steps.
Basic syntax
The syntax is:
os.chdir(path)
pathis the folder you want Python to switch to- The path can be relative or absolute
- You must import the
osmodule first
Example:
import os
os.chdir("my_folder")
One important detail: os.chdir() does not return the new path. It only changes the current directory.
Simple example
This example shows the current folder, changes to another folder, and then shows the new folder.
import os
print("Before:", os.getcwd())
os.chdir("my_folder")
print("After:", os.getcwd())
Example output:
Before: /Users/alex/project
After: /Users/alex/project/my_folder
What the code does
os.getcwd()gets the current working directoryos.chdir("my_folder")changes into a folder namedmy_folder- The second
os.getcwd()confirms the change
This only works if my_folder exists inside the current folder.
Using relative and absolute paths
You can use os.chdir() with either a relative path or an absolute path.
Relative path
A relative path is based on the current working directory.
import os
os.chdir("my_folder")
print(os.getcwd())
This means:
- Start from the current folder
- Look for a folder called
my_folder - Move into it
If the current folder is not what you expect, the relative path may fail.
Absolute path
An absolute path gives the full location of the folder.
import os
os.chdir("/Users/alex/project/my_folder")
print(os.getcwd())
On Windows, it might look like this:
import os
os.chdir(r"C:\Users\Alex\project\my_folder")
print(os.getcwd())
Absolute paths are often easier to debug because they do not depend on the current folder. If file paths are still confusing, see working with file paths in Python.
Why beginners use os.chdir()
Beginners often use os.chdir() for simple tasks like these:
- To make file reading and writing easier in small scripts
- To switch into a project folder before listing files
- To work with folders step by step while learning
Example:
import os
os.chdir("my_folder")
print(os.listdir())
This changes into my_folder and then lists the files inside it.
For small practice scripts, this can be convenient. In larger programs, changing directories too often can make code harder to understand.
Common errors when using os.chdir()
A few common errors happen when the path is wrong or inaccessible.
FileNotFoundError
This happens when the folder does not exist.
import os
os.chdir("missing_folder")
Python may show an error like:
FileNotFoundError: [Errno 2] No such file or directory: 'missing_folder'
See how to fix FileNotFoundError: No such file or directory.
NotADirectoryError
This happens when the path points to a file instead of a folder.
import os
os.chdir("notes.txt")
If notes.txt is a file, not a directory, Python raises this error.
See how to fix NotADirectoryError.
PermissionError
This happens when Python does not have permission to enter the folder.
import os
os.chdir("/root")
Some system folders are restricted.
See how to fix PermissionError: permission denied.
Safer ways to work with paths
os.chdir() is useful, but it helps to be careful.
Here are some safer habits:
- Check the current folder with
os.getcwd()before changing it - Use
os.path.exists()to check whether a path exists - Consider using full paths when debugging
- Avoid changing directories many times in one script
Example:
import os
folder = "my_folder"
print("Current folder:", os.getcwd())
print("Exists:", os.path.exists(folder))
if os.path.exists(folder):
os.chdir(folder)
print("Changed to:", os.getcwd())
else:
print("Folder not found")
You can learn more about os.path.exists() if you want to check paths before using them.
Common mistakes
These are the most common causes of problems with os.chdir():
- Trying to change to a folder that does not exist
- Using the wrong relative path
- Forgetting which folder the script started in
- Passing a file path instead of a directory path
- Using a path that Python does not have permission to access
Helpful debugging commands:
import os; print(os.getcwd())
import os; print(os.path.exists('my_folder'))
import os; print(os.listdir())
import os; print(os.path.isdir('my_folder'))
These commands help you answer basic questions:
- Where am I right now?
- Does this path exist?
- What files and folders are here?
- Is this path a directory?
FAQ
What does os.chdir() return?
It returns None. It changes the current working directory but does not return the new path.
How do I check the current working directory?
Use os.getcwd() to print the current working directory.
Can I use os.chdir() with a relative path?
Yes. Python will interpret the path relative to the current working directory.
Why does os.chdir() break my file code?
Changing the working directory changes how relative paths are resolved, so files may no longer be found where your code expects them.