相关疑难解决方法(0)

os.path.dirname(__ file__)返回空

我想获取当前目录的路径,在该目录下执行.py文件.

例如,一个D:\test.py包含代码的简单文件:

import os

print os.getcwd()
print os.path.basename(__file__)
print os.path.abspath(__file__)
print os.path.dirname(__file__)
Run Code Online (Sandbox Code Playgroud)

输出结果是奇怪的:

D:\
test.py
D:\test.py
EMPTY
Run Code Online (Sandbox Code Playgroud)

我期待从相同的结果getcwd()path.dirname().

鉴于os.path.abspath = os.path.dirname + os.path.basename,为什么

os.path.dirname(__file__)
Run Code Online (Sandbox Code Playgroud)

返回空?

python

148
推荐指数
6
解决办法
18万
查看次数

标签 统计

python ×1