Content · Programming · Python

Python os.getcwd() Function: Usage and Examples

Get the current working directory using os.getcwd(). os.getcwd() returns the process current working directory as a string.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This Python tutorial explains os.getcwd() Function: Usage and Examples. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.

What This Feature Does


Get the current working directory using os.getcwd().

os.getcwd() returns the process current working directory as a string. Relative file paths resolve against this directory, which can differ from the script location. os.getcwdb() provides a bytes representation when needed.

When to Use It


This technique belongs to operating-system paths and metadata. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.

Step-by-Step Python Example


Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.

import os
from pathlib import Path

working_directory = os.getcwd()
print(Path(working_directory).is_absolute())
print(working_directory == str(Path.cwd()))

Understanding the Code


  • First, the imports make os, pathlib available. They provide the filesystem, data or standard-library functions used later.
  • Find the call os.getcwd(). Its parameters determine the specific operation demonstrated in the title.
  • Its purpose is to get the current working directory using os.getcwd(). Compare this description against the result printed by the program.
  • The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.

Expected Output


True
True

Each printed line corresponds to a separate observation in the program. Use the lines to distinguish returned data, changes to existing objects, and status checks.

Important Details and Edge Cases


Use Path(__file__).resolve().parent when locating resources next to a script file.

The current directory belongs to the process and can differ when a script is run through an IDE, scheduled job or terminal.

Practice: Try It Yourself


Run the same script from two terminal directories and compare the results.

Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.

Official Python Documentation


For the complete reference, see the Python standard library documentation.

Common Beginner Mistakes


  • Treating getcwd() as the directory that always holds the executing source file.
  • Assuming the current working directory never changes during a program.
  • The current directory belongs to the process and can differ when a script is run through an IDE, scheduled job or terminal.

Key Takeaways


  • Get the current working directory using os.getcwd().
  • os.getcwd() returns the process current working directory as a string. Relative file paths resolve against this directory, which can differ from the script location. os.getcwdb() provides a bytes representation when needed.
  • Avoid this common pitfall: Treating getcwd() as the directory that always holds the executing source file.
Share this resourceSend this page to someone who may find it useful.