Python 如何输出运行路径而不是位置路径?

Python 如何输出运行路径而不是位置路径?,python,Python,我有以下资料: % more a.py import os os.system('pwd') % python a.py /Users/yl/test/f % cd .. % python ./f/a.py /Users/yl/test 基本上,我希望最后的输出是“/Users/yl/test/f”,这是脚本所在的路径(而不是调用python的地方)。他们四处游荡,但没有找到好的解决办法。谢谢你的建议 import os app_dir = os.path.dirname(__fi

我有以下资料:

% more a.py 
import os
os.system('pwd')

% python a.py 
/Users/yl/test/f

% cd ..

% python ./f/a.py 
/Users/yl/test
基本上,我希望最后的输出是“/Users/yl/test/f”,这是脚本所在的路径(而不是调用python的地方)。他们四处游荡,但没有找到好的解决办法。谢谢你的建议

import os
app_dir = os.path.dirname(__file__)
print(app_dir)
import os
print (os.path.dirname(os.path.abspath(__file__)))