我不知道为什么我的 valex.exists(path)
不断返回 None
,我的意思是它在单独测试时工作正常,但在另一个模块调用它时工作正常使用时,尽管路径名有效,但它仍然返回 None
。
这是我的valex.exists(path)
:
def exists(path):
# I've imported os already
if os.path.exists(path):
return "Exists"
else:
return None
最佳答案
对于这些类型的函数(我同意你的函数没有用并且返回 None 是多余的这一事实,但一般来说),我认为使用一行 if 语句更清晰:
def exists(path):
return "Exists" if os.path.exists(path) else None
关于python - 即使预期返回 'None',仍继续返回 'True',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12062974/