How to get the current working directory in Python using os.getcwd()?

Published on Aug. 22, 2023, 12:17 p.m.

You can use the os.getcwd() method in Python to get the current working directory. Here’s an example code snippet that shows how to get the current working directory:

import os

# get the current working directory
current_dir = os.getcwd()

# print the current working directory
print(current_dir)

This code will retrieve the current working directory using os.getcwd() and store it in the variable current_dir. Then, it will print out the current working directory to the console.

Note that os.getcwd() returns a string containing the current working directory. If you need to perform any operations on the current working directory, you can use this string directly or store it in a variable as shown above.

Tags: