PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteUse os.getcwd() to get Python’s current working directory as a string, or Path.cwd() to get the same directory as a pathlib.Path object. Both report the process’s working directory; neither automatically reports the folder containing your Python file.
Get the current working directory
For a string result, use os.getcwd():
import os
current_directory = os.getcwd()
print(current_directory)
For a Path object, use Path.cwd():
from pathlib import Path
current_directory = Path.cwd()
print(current_directory)
Python’s pathlib documentation describes Path.cwd() as returning a path object for the current directory as returned by os.getcwd(). The os documentation covers os.getcwd().
As an Amazon Associate I earn from qualifying purchases.
What “current directory” means
The current working directory (cwd) is the process context used to resolve relative paths. If you open data.csv using a relative path, Python looks for it relative to the cwd—not necessarily beside the source file.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →The cwd can differ depending on how the program was started: for example, a terminal, IDE, notebook, test runner, or another program may set a working directory that is different from the script’s location. To check where a relative lookup is anchored, print Path.cwd() or os.getcwd().
#1 Best Overall
Choose between os.getcwd() and Path.cwd()
| Need | Use | Result |
|---|---|---|
A string path, such as for existing os-based code or an API that expects a string |
os.getcwd() |
str |
| A path object for composing paths or using pathlib methods | Path.cwd() |
pathlib.Path |
The calls report the same kind of location; choose based on the type your surrounding code uses. The pathlib documentation maps os.getcwd() to Path.cwd().
Build a path inside the current directory
With pathlib, combine the cwd with child directories or filenames using the / operator:
Rank #2
from pathlib import Path
input_file = Path.cwd() / "data" / "input.csv"
This creates a path under the process’s current working directory. It does not change that directory or make it relative to the Python file.
Get the directory containing a script instead
If your goal is the folder containing a script, use the script’s location rather than cwd(). In ordinary script execution, __file__ identifies the script file; with pathlib, you can derive its parent directory:
from pathlib import Path
script_directory = Path(__file__).resolve().parent
This is a different question from asking for the process’s current working directory. In environments such as interactive notebooks, __file__ may not be available.
Diagnose a file that Python cannot find
If open("data.csv") or Path("data.csv") targets an unexpected location, inspect the cwd and compare it with the location where the file actually lives:
from pathlib import Path
print("Working directory:", Path.cwd())
print("Relative path resolves to:", Path("data.csv").resolve())
A relative path is anchored to the process cwd. If the file is beside the script but the cwd is elsewhere, use a path derived from the script location or set the working directory deliberately in the program’s launch configuration.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




