获取Python 脚本的所在目录

来源:互联网 发布:淘宝花卉种子 编辑:程序博客网 时间:2024/06/05 16:30

获取Python 脚本的所在目录

Tips: 一些工程开发中,使用相对路径,能提高工程的可移植性(如Python/shell的脚本工程)

  • 获取Python脚本的所在目录的方法
    You need to call os.path.realpath on file, so that when file is a filename without the path you still get the dir path:
import osprint os.path.dirname(os.path.realpath(__file__))
  • 参考链接
0 0
原创粉丝点击