为什么 python 脚本在 Spyder 与 cmd 提示符下的工作方式不同
Why does python script works differently in Spyder vs cmd prompt
我有以下脚本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语句成功,但我想知道的是为什么两个环境会产生不同的结果。
os.path.exists()
开始在 Python 3.6 中接受路径对象,您的问题出现在您的 cmd 提示符中,因为它是 运行 Python 3.5,将其更改为 3.6 以解决你的问题。
我有以下脚本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语句成功,但我想知道的是为什么两个环境会产生不同的结果。
os.path.exists()
开始在 Python 3.6 中接受路径对象,您的问题出现在您的 cmd 提示符中,因为它是 运行 Python 3.5,将其更改为 3.6 以解决你的问题。