Python获取当前文件所在路径

Python获取当前正在执行的文件所在路径

#! /usr/bin/env python

import os

filepath = __file__
realpath = os.path.realpath(filepath)
current_path = os.path.dirname(realpath)

print(filepath)
print(realpath)
print(current_path)

执行结果:

$ python fileDemo.py 
fileDemo.py
/home/username/Documents/fileDemo.py
/home/username/Documents


你可能感兴趣的:(Python)