为什么python脚本在Spyder和cmd promp中的工作方式不同

2024-10-03 04:37:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下脚本测试.py

import pathlib, os

path = "C:\\Windows"
pathparent = pathlib.Path("C:\\Windows").parent

if os.path.exists(pathparent):
    print("path exists")

当我在Spyder IDE中执行它时,我得到的是:

^{pr2}$

)我知道:

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语句成功,但我想知道的是为什么这两个环境会产生不同的结果。在


Tags: pathinpyimport脚本ifoswindows