我有以下脚本test.py:
import pathlib, os
path = "C:\\Windows"
pathparent = pathlib.Path("C:\\Windows").parent
if os.path.exists(pathparent):
print("path exists")
当我在 Spyder IDE 中执行它时,我得到了这个:
path exists
当我从命令提示符(python test.py)运行它时,我得到了这个:
Traceback (most recent call last):
File "test.py", line 6, in <module>
if os.path.exists(pathparent):
File "C:\Anaconda3\lib\genericpath.py", line 19, in exists
os.stat(path)
TypeError: argument should be string, bytes or integer, not WindowsPath
知道为什么我会得到不同的结果吗?
注意:我知道在str()中包装pathparent会使if语句成功,但我想知道为什么这两个环境会产生不同的结果。