如何知道python运行脚本的路径? [英] How to know the path the script the python run?

查看:204
本文介绍了如何知道python运行脚本的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sys.arg [0]给了我python脚本.例如,"python hello.py"为sys.arg [0]返回hello.py.但是我需要知道hello.py在完整路径中的位置.

sys.arg[0] gives me the python script. For example 'python hello.py' returns hello.py for sys.arg[0]. But I need to know where the hello.py is located in full path.

如何使用python做到这一点?

How can I do that with python?

推荐答案

import sys
print(sys.path[0])

文档:

在程序启动时初始化, 此列表的第一项sys.path [0], 是包含脚本的目录 用来调用Python的 解释器.

As initialized upon program startup, the first item of this list, sys.path[0], is the directory containing the script that was used to invoke the Python interpreter.

这篇关于如何知道python运行脚本的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆