How to Get the Path of the Running File in Python?

Have you ever needed to know where your Python script is located? Perhaps you want to load data from a relative directory or output logs to the same place. In this short post, I’ll show you how to get the path of the running file in Python.

There are a few ways to get the path of the running file. The most common is to use the special variable file. This variable is automatically initialized by Python when your script starts running.

Get the path (location) of the current file, i.e., currently running (.py) script file with __file__.

For example, let’s say your script is located at /Users/david/Documents/ file contains:

import os print('getcwd: ', os.getcwd()) print('__file__: ', __file__)
Code language: Python (python)

When you run this script in the terminal or command line, it prints out the value of the file to confirm this:

$ /usr/bin/python3 /Users/david/Documents/ getcwd: /Users/david __file__: /Users/david/Documents/
Code language: JavaScript (javascript)

Keep in mind that file only works if your script is being run directly.

If your script is being called as a module by another script, then file will be set to the path of the parent script.

Another way to get the path of the running file is to use the os module.

The os module has a number of useful functions for interacting with your operating system.

One of these functions is os.path.realpath(). This function will return the path of the running file regardless of how it’s being run.

For example:

The content of the file as below

import os print('getcwd: ', os.getcwd()) print('__file__: ', __file__) print(os.path.realpath(__file__)) # /Users/david/Documents/
Code language: PHP (php)

Run the script, and you get

$ /usr/bin/python3 /Users/david/Documents/ getcwd: /Users/david __file__: /Users/david/Documents/ /Users/david/Documents/
Code language: JavaScript (javascript)

os.path.realpath() also has one limitation that file does not: it does not work on Windows if your Python script contains non-ASCII characters in its path name. If you need compatibility with Windows, then you should stick with the file.

Determine the name of the file being executed as well as the directory in which it is located.

print('basename: ', os.path.basename(__file__)) print('dirname: ', os.path.dirname(__file__)) # Output # # /Users/david/Documents
Code language: Python (python)

If you want to get only the directory name directly above the file, use:

print(os.path.basename(os.path.dirname(__file__))) # Output:Documents
Code language: Python (python)

If you get the relative path with the __file__, you can convert it to an absolute path with os.path.abspath() method.

print('Your abspath: ', os.path.abspath(__file__)) print('Your abs dirname: ', os.path.dirname(os.path.abspath(__file__)))
Code language: PHP (php)

If you want to read other files based on where the current script file is (its path), you can use os.path.join() to join the directory path of the current file and the relative path (from the current file) of the file you want to read.

path_target_file = os.path.join(os.path.dirname(__file__), 'target.txt') # /Users/david/Documents/target.txt
Code language: PHP (php)

You can change the current directory to the directory of the current script file with this script:

os.chdir(os.path.dirname(os.path.abspath(__file__))) print('getcwd: ', os.getcwd())
Code language: PHP (php)

In this blog post, we’ve shown you how to get the path of the running file in Python. We hope you found this post helpful!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts