How to get the current file path in Python?

In Python, you can use the os module’s path attribute to obtain the current file’s path.

Here is an example code:

import os

# 获取当前文件的路径
current_path = os.path.abspath(__file__)
print(current_path)

Executing the above code will output the absolute path of the current file.

Please note that this method is only applicable to script files, and may return an empty string if used in an interactive environment.

bannerAds