[英]Python script works in Spyder and CMD but not Atom or Git Bash
[英]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中接受路径对象,并且您的问题在运行Python 3.5的cmd提示中出现,请将其更改为3.6以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.