Python CD Command: How to Change Directory in Python
If you’ve been searching for the Python CD command, you’re probably trying to change folders while working in Python, either inside a script or in the terminal. Many beginners assume that cd is a Python command. However, that’s not exactly true.
The cd command belongs to your operating system’s shell (Command Prompt, PowerShell, Bash, Terminal), not the Python language itself.
So, how do you change directories in Python correctly?
In this complete guide, you’ll learn:
- Whether Python has a cd command
- How to use the cd command before running Python
- How to change directory inside a Python script
- The difference between shell navigation and Python navigation
- Common errors and how to fix them
- Best practices for working with file paths
Let’s clear up the confusion once and for all.
What Is the CD Command and Does Python Support It?
What Does the CD Command Do?
The cd command stands for change directory.
It is a built-in command in operating system shells like:
- Windows Command Prompt
- PowerShell
- macOS Terminal
- Linux Bash
Example (Terminal):
cd Documents
This command moves you from your current folder into the “Documents” folder.
The cd command affects the shell session, not Python directly.
Is There a Python CD Command?
Short answer:
There is no built-in CD command in Python.
If you type this inside a Python script:
cd Documents
You will get an error.
That’s because cd is not a Python keyword or function.
Instead, Python provides a proper method to change directories using the os module.
How to Change Directory in Python Using os.chdir() (Step-by-Step)
To change directories inside Python, you use:
os.chdir()
This function changes the working directory of the current Python process.

Step 1: Import the os Module
import os
Step 2: Change Directory
os.chdir(“Documents”)
Now Python will operate inside that folder.
Step 3: Verify the Current Directory
You can check the current working directory using:
os.getcwd()
Example:
import os
print(“Current directory:”, os.getcwd())
os.chdir(“Documents”)
print(“New directory:”, os.getcwd())
This confirms the directory change.
What Is the Current Working Directory in Python?
The working directory is the folder where Python:
- Reads files
- Saves files
- Executes relative paths
If your script can’t find a file, the working directory is usually the issue.
That’s why understanding the Python cd command concept is important.
Python CD Command in Terminal vs Inside Script
This is where most confusion happens.
There are two scenarios:
Using CD Before Running Python
You can use the cd command in your terminal before launching Python.
Example:
cd Desktop
python myscript.py
Here:
- cd Desktop is a shell command
- python myscript.py runs your script
This is the most common workflow.

Changing Directory Inside a Python Script
If you want Python to switch folders while running:
import os
os.chdir(“C:/Users/John/Desktop”)
This changes the directory only for that Python process.
It does not affect your terminal session.
Absolute vs Relative Paths in Python
When using os.chdir(), you can use:
Absolute Path
Full path from root:
os.chdir(“C:/Users/John/Desktop”)
Relative Path
Based on the current directory:
os.chdir(“Documents”)
Relative paths are cleaner and more portable.
Windows vs macOS vs Linux Path Differences
Windows
Uses backslashes:
C:\Users\John\Desktop
In Python, you must either:
- Use double backslashes
- Use forward slashes
- Use raw strings
Correct examples:
os.chdir(“C:/Users/John/Desktop”)
OR
os.chdir(r”C:\Users\John\Desktop”)
macOS / Linux
Use forward slashes:
os.chdir(“/Users/john/Desktop”)
No escaping required.
Using pathlib (Modern Alternative)
Modern Python developers often prefer pathlib.
Example:
from pathlib import Path
path = Path(“Documents”)
print(path.resolve())
However, to change directory, you still use os.chdir().
Pathlib is mainly used for:
- Path manipulation
- Cleaner syntax
- Cross-platform compatibility
Common Errors When Using the Python CD Command
Let’s troubleshoot common issues.
FileNotFoundError
Error:
FileNotFoundError: No such file or directory
Cause:
- Wrong path
- Folder does not exist
- Typo
Fix:
- Verify the path manually
- Print os.getcwd()
- Check spelling
PermissionError
Error:
PermissionError: Access denied
Cause:
- Restricted system folder
- No read/write permissions
Fix:
- Run the terminal as an administrator
- Choose an accessible folder
Incorrect Slash Format
Windows users often forget escape characters.
Wrong:
os.chdir(“C:\Users\John\Desktop”)
Correct:
os.chdir(“C:/Users/John/Desktop”)
OR
os.chdir(r”C:\Users\John\Desktop”)
How to Go Back One Directory in Python
In shell:
cd ..
In Python:
os.chdir(“..”)
This moves up one folder.
Best Practices for Changing Directory in Python
- Avoid hard-coded absolute paths
- Use relative paths when possible
- Always check the current directory with os.getcwd()
- Handle errors using try-except
- Use pathlib for path construction
Example with error handling:
import os
try:
os.chdir(“Documents”)
except FileNotFoundError:
print(“Folder not found.”)
Advanced Tip: Avoid Changing Directory Altogether
In professional applications, many developers avoid changing the working directory entirely.
Instead, they use full paths:
with open(“Documents/file.txt”) as f:
content = f.read()
This keeps the working directory stable.
Temporarily Changing Directory (Best Practice)
Sometimes you want to change the directory temporarily and then return.
Example:
import os
original_dir = os.getcwd()
os.chdir(“Documents”)
print(“Now in:”, os.getcwd())
os.chdir(original_dir)
print(“Back to:”, os.getcwd())
This prevents side effects in larger applications.
Python CD Command in Virtual Environments
When working with a virtual environment:
cd project_folder
venv\Scripts\activate
The cd command here is still a shell command.
Inside Python scripts, use os.chdir() as usual.
Real-World Use Cases
Understanding the Python CD command concepts is useful in:
Data Science Projects
Reading datasets from different folders.
Automation Scripts
Processing files in multiple directories.
Web Scraping
Saving output files to specific locations.
Deployment Scripts
Changing directory before running builds.
Python CD Command vs Shell CD Command
| Feature | Shell CD | Python os.chdir() |
| Scope | Terminal session | Python process only |
| Environment | OS shell | Python runtime |
| Syntax | cd folder | os.chdir(“folder”) |
| Persistence | Until the terminal closes | Until the script ends |
Understanding this difference prevents confusion.
Can You Use a CD Inside a Jupyter Notebook?
Yes — but differently.
In Jupyter, you can use:
%cd foldername
This is a magic command specific to Jupyter.
It is not standard Python syntax.
Why CD Command Python Confusion Happens
Beginners often:
- Open Python interpreter
- Type cd foldername
- See error
- Assume Python is broken
But Python is not a shell.
The confusion comes from mixing terminal commands with programming language syntax.
Python CD Command for Beginners (Step-by-Step Workflow)
- Open terminal
- Use cd to navigate to the project folder
- Run Python script
- Use os.chdir() only if needed inside the script
- Verify working directory
This workflow works in Windows, macOS, and Linux.
Conclusion: How to Use CD Command in Python
The term ” Python cd command is slightly misleading because Python does not include a built-in cd command as the shell does.
Instead:
- Use cd in your terminal to navigate before running scripts
- Use os.chdir() to change the directory inside Python
- Always verify the working directory
- Handle errors carefully
- Prefer relative paths for portability
Once you understand the difference between shell commands and Python functions, directory navigation becomes simple and predictable.
Mastering this concept will save you hours of debugging in file handling, automation, and data processing projects. Automate scripts with automation software for tasks.
