os.getcwd() Function Explained
os.getcwd() returns the current working directory in Python.
This is useful when you want to know which folder Python is currently using. It matters because relative file paths are based on this folder. If your code opens or saves files using names like "data.txt", Python will look in the current working directory unless you give a full path.
Quick answer
import os
current_folder = os.getcwd()
print(current_folder)
Use os.getcwd() to get the current working directory as a string.
What os.getcwd() does
os.getcwd():
- Returns the current working directory
- Gives the result as a string path
- Shows the folder Python is currently using
- Is often helpful before opening or saving files
In simple terms, it tells you: “What folder is Python working in right now?”
Basic example
First, import the os module. Then call os.getcwd() with no arguments.
import os
current_folder = os.getcwd()
print(current_folder)
What this code does
import osmakes theosmodule availableos.getcwd()gets the current working directoryprint(current_folder)shows that path on the screen
Example output
On Windows, you might see:
C:\Users\Alice\Projects
On macOS or Linux, you might see:
/home/alice/projects
The exact result depends on where Python is running.
Why the current working directory matters
The current working directory is important because relative paths depend on it.
For example:
with open("notes.txt", "r") as file:
print(file.read())
This code does not look everywhere on your computer for notes.txt. It looks in the current working directory.
If Python is running in a different folder than you expected, you may get a file error.
Debugging example
import os
print("Current working directory:", os.getcwd())
with open("notes.txt", "r") as file:
print(file.read())
If the file cannot be found, printing os.getcwd() helps you check where Python is looking.
This is especially helpful when running code from:
- A terminal
- An IDE
- A code editor run button
- A notebook environment
If you are working with file paths, see working with file paths in Python.
os.getcwd() vs script location
Beginners often confuse the current working directory with the folder where the Python file is saved.
They are not always the same.
os.getcwd()gives the current working folder- It does not necessarily give the script file location
For example, suppose your file is saved here:
/home/alice/projects/app/script.py
But you run it from this folder:
/home/alice
Then os.getcwd() may return:
/home/alice
Even though the script itself is stored in /home/alice/projects/app.
This difference is common in IDEs, where the program may start from a project folder, workspace folder, or another configured location.
Common beginner use cases
Here are a few practical ways beginners use os.getcwd():
- Check where Python is looking for files
- Build a full path before opening a file
- Print the folder while debugging
- Confirm the environment before changing directories
Example:
import os
folder = os.getcwd()
filename = "report.txt"
full_path = os.path.join(folder, filename)
print(full_path)
This example uses os.path.join() to build a path safely.
Related functions to know
os.getcwd() is often used with other path-related tools:
os.chdir()changes the current working directoryos.path.join()combines folder and file names safelyos.path.exists()checks whether a path exists
Example:
import os
print("Before:", os.getcwd())
if os.path.exists("myfile.txt"):
print("File found")
else:
print("File not found")
If you want a broader view of path and system tools, read the Python os module overview.
Common mistakes
These are common beginner mistakes when using os.getcwd():
- Using a relative file path without checking the current working directory
- Assuming
os.getcwd()returns the script file location - Forgetting to import
osbefore callingos.getcwd() - Running the same script from different folders and getting different results
A very common problem looks like this:
import os
print(os.getcwd())
with open("myfile.txt", "r") as file:
print(file.read())
If this fails, the file may not be in the folder shown by os.getcwd().
You can also run quick checks from the command line:
python script.py
pwd
cd /path/to/folder
python -c "import os; print(os.getcwd())"
python -c "import os; print(os.path.exists('myfile.txt'))"
If your program cannot find a file, see FileNotFoundError: Errno 2 No such file or directory.
FAQ
What does os.getcwd() return?
It returns the current working directory as a string.
Does os.getcwd() return the script location?
No. It returns the current working directory, which may be different from the script folder.
Do I need to pass an argument to os.getcwd()?
No. It takes no arguments.
Why does my file open in one place but not another?
Your current working directory may be different, so a relative path points to a different folder.