How to print full path of current file's directory in Python?

PythonServer Side ProgrammingProgramming

To get current file's full path, you can use the os.path.abspath function. If you want only the directory path, you can call os.path.dirname.

Example

import os
absolute_path = os.path.abspath(__file__)
print("Full path: " + absolute_path)
print("Directory Path: " + os.path.dirname(absolute_path))

Output

This will give the output:

Full path: C:\\python27\\test.py
Directory path: C:\\python27\\
raja
Published on 27-Dec-2017 12:01:16
Advertisements