os.path.dirname(__file__)返回脚本的路径,但是需要注意一下几点:
1、必须是实际存在的.py文件,如果在命令行执行,则会引发异常NameError: name '__file__' is not defined
2、在运行的时候如果输入完整的执行的路径,则返回.py文件的全路径如:
python D:/test/test.py 则返回路径 D:/test ,如果是python test.py 则返回空
os.path.dirname(__file__)返回脚本的路径,但是需要注意一下几点:
1、必须是实际存在的.py文件,如果在命令行执行,则会引发异常NameError: name '__file__' is not defined
2、在运行的时候如果输入完整的执行的路径,则返回.py文件的全路径如:
python D:/test/test.py 则返回路径 D:/test ,如果是python test.py 则返回空