Understanding the getcwd() Method in Python

In this tutorial, we will be exploring the getcwd() method in Python. This is a built-in function provided by the os module, which allows us to interact with the underlying operating system in several different ways.

What is getcwd()?

The getcwd() stands for ‘Get Current Working Directory’. It returns the current working directory of a process. In simpler terms, it tells you in which folder your python script is running.


import os

Tutorial: Using getcwd()

To use getcwd(), you first need to import the os module. Here’s how:

import os

This code will output something like this:


This means that your current python script is running inside the ‘/home/user/my_folder/Python_Scripts’ directory.

Proper Usage of getcwd()

The getcwd() method can be very useful when you are dealing with file paths. For instance, if you want to read a file that resides in the same directory as your script, instead of specifying its full path, you can use getcwd().

import os

# Get current working directory
current_directory = os.getcwd()

# Create complete filepath
file_path = current_directory + "/my_file.txt"

# Open and read file
with open(file_path) as f:

This code will read the file ‘my_file.txt’ that resides in the same directory as your script.


The getcwd() method is a simple yet powerful tool in Python. It allows you to interact with the file system and make your code more flexible and portable. Remember, always import the os module before using getcwd().

Leave a Reply

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