As a developer working on a project for a US-based company, I recently encountered a situation where I needed to access files relative to the current script’s location. In this article, I will explain how to get the path of the current file in Python. Let us explore different methods to achieve this task with examples.
Get the Path of the Current File in Python
Before getting into the code, let’s clarify what we mean by the “current file’s path”. when you execute a Python script, it has a specific location on your filesystem. This location is known as the “current working directory” or “present working directory.” The current file’s path refers to the absolute path of the directory containing the Python script you are currently executing.
Read How to Replace a Specific Line in a File Using Python?
Method 1: Use os.path.abspath(__file__)
One of the most straightforward ways to get the current file’s path is by using the os.path.abspath() function in combination with the __file__ variable. Here’s an example:
import os
current_file_path = os.path.abspath(__file__)
print("Current file path:", current_file_path)Output:
Current file path: c:\Users\Public\code\example.pyI have executed the above example code and added the screenshot below.

In this code snippet, we first import the os module, which provides a way to interact with the operating system. We then use os.path.abspath(__file__) to get the absolute path of the current file. The __file__ variable is a special variable in Python that holds the path of the currently executing script.
Check out How to Call a Function from Another File in Python?
Method 2: Use pathlib.Path(__file__).resolve()
Another approach to get the current file’s path is by utilizing the pathlib module, which provides an object-oriented way to handle file paths. Here’s an example:
from pathlib import Path
current_file_path = Path(__file__).resolve()
print("Current file path:", current_file_path)Output:
Current file path: c:\Users\Public\code\example.pyI have executed the above example code and added the screenshot below.

In this code, we import the Path class from the pathlib module. We then use Path(__file__).resolve() to obtain the absolute path of the current file. The resolve() method resolves any symbolic links and returns the absolute path.
Read How to Read an Excel File in Python?
Get the Directory Path
Often, you may need to get the directory path of the current file instead of the file path itself. To achieve this, you can use the os.path.dirname() function or the parent attribute of the Path object.
Method 1. Use os.path.dirname()
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
print("Current directory:", current_dir)Output:
Current directory: c:\Users\Public\codeI have executed the above example code and added the screenshot below.

In this example, we use os.path.dirname() to extract the directory path from the absolute file path obtained using os.path.abspath(__file__).
Read How to Import a Python File from the Same Directory?
Method 2. Use pathlib.Path().parent
from pathlib import Path
current_dir = Path(__file__).resolve().parent
print("Current directory:", current_dir)Here, we use the parent attribute of the Path object to get the parent directory of the current file.
Check out How to Get File Size in Python?
Example
Let’s consider a real-world scenario where getting the current file’s path is useful. Suppose you are working on a project for a US-based e-commerce company, and you need to read a configuration file located in the same directory as your Python script.
import os
import json
# Get the current file's directory
current_dir = os.path.dirname(os.path.abspath(__file__))
# Construct the path to the configuration file
config_path = os.path.join(current_dir, "config.json")
# Read the configuration file
with open(config_path, "r") as file:
config = json.load(file)
# Access the configuration values
api_key = config["api_key"]
database_url = config["database_url"]
print("API Key:", api_key)
print("Database URL:", database_url)In this example, we use os.path.dirname(os.path.abspath(__file__)) to get the current directory. We then construct the path to the configuration file named config.json using os.path.join(). By joining the current directory with the configuration file name, we ensure that the script can locate the file regardless of the current working directory.
Next, we open the configuration file and parse its JSON content using json.load() , and access the values of api_key and database_url. This demonstrates how getting the current file’s path enables you to access files relative to the script’s location.
Read How to Overwrite a File in Python?
Conclusion
In this tutorial, I explained how to get the path of the current file in Python. I discussed two methods to achieve this task such as using os.path.abspath(__file__) and pathlib.Path(__file__).resolve(), I also covered how to extract the directory path from the file path using os.path.dirname() and pathlib.Path().parent and a real-world example.
You may like to read:
- How to Write a Variable to a File in Python?
- How to Check if a File is Empty in Python?
- How to Rename Files in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.