[英]Why does python script works differently in Spyder vs cmd prompt
I have the following script test.py : 我有以下脚本test.py :
import pathlib, os
path = "C:\\Windows"
pathparent = pathlib.Path("C:\\Windows").parent
if os.path.exists(pathparent):
print("path exists")
and when I execute it in Spyder IDE I get this: 当我在Spyder IDE中执行它时,我得到了:
path exists
when I run it from the command prompt ( python test.py ) I get this: 当我从命令提示符( 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
Any idea why I get the different results? 知道为什么我会得到不同的结果吗?
Note: I know that wrapping pathparent in str() will make the if statement succeed, but what I want to know is why the the two environments yield different results. 注意:我知道在str()中包装pathparent将使if语句成功,但是我想知道的是为什么两个环境产生不同的结果。
os.path.exists()
开始在Python 3.6中接受路径对象,并且您的问题在运行Python 3.5的cmd提示中出现,请将其更改为3.6以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.