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:
We import the
os
module to work with operating system-related functions.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.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
Post a Comment