How do I get the full path of the current file's directory?
You can use the os
module to get the full path of the current file's directory. The os.path
module provides several useful functions for working with file paths.
In Python, the __file__
attribute of the builtins
module is the path of the current file. So you can use the os.path.dirname()
function to get the directory containing the current file, and then use os.path.abspath()
to get the full path of the directory.
import os
current_file_directory = os.path.dirname(os.path.abspath(__file__))
print(current_file_directory)
Watch a video course
Python - The Practical Guide
You can also use os.path.realpath
which will also follow symlinks (if any) and give the actual location of the file on the filesystem
import os
current_file_directory = os.path.dirname(os.path.realpath(__file__))
print(current_file_directory)
This will print the full path of the current file's directory to the console.