How do I get the full path of the current file's directory?

 

You can get the full path of the current file's directory in Python using the os module. Specifically, you can use os.path.dirname(__file__) to obtain the directory path of the currently executing script. Here's an example:

python
import os # Get the full path of the current file's directory current_directory = os.path.dirname(__file__) # Print the current directory print("Current directory:", current_directory)

In this example:

  1. We import the os module to work with operating system-related functions.

  2. os.path.dirname(__file__) is used to get the directory path of the currently executing script. __file__ is a built-in variable in Python that represents the path of the current script.

  3. We then print the obtained directory path, which will be the full path to the directory containing the script.

When you run this code, it will print the full path of the current file's directory to the console.

Comments