October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Get the Current Directory in Python: `os.getcwd()` and `Path.cwd()`

Use os.getcwd() for a string or Path.cwd() for a pathlib object. Both report the process working directory, which may differ from the directory containing your script.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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().

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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.