1

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

4

1 回答 1

2

os.path.exists()开始接受 Python 3.6 中的路径对象,并且您的问题出现在您的 cmd 提示符中,因为它正在运行 Python 3.5,请将其更改为 3.6 以解决您的问题。

于 2017-05-27T17:31:16.490 回答