简体   繁体   English

为什么python脚本在Spyder vs cmd提示符下的工作方式不同

[英]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.

相关问题 Python 脚本适用于 Spyder 和 CMD 但不适用于 Atom 或 Git Z7024FF0860776AB1719ZD783 - Python script works in Spyder and CMD but not Atom or Git Bash 为什么 python 脚本在 pycharm 中运行和在命令提示符下运行时的行为不同? - Why does a python script behaves differently when it is run in pycharm and when it is run from a command prompt? Python 底图脚本与 Anaconda cmd 提示符一起运行,但不与 VS Code 一起运行 - Python Basemap script runs with Anaconda cmd prompt but not with VS Code UnicodeEncodeError:脚本在Spyder和IDLE上运行,但不在cmd提示符下运行 - UnicodeEncodeError: Script runs on Spyder and IDLE, yet not on cmd prompt Python脚本无法在bash脚本中运行,但在cmd提示符下可以正常工作 - Python script won't run in bash script, but works fine in cmd prompt 使用ansible时为什么Python脚本的工作方式不同? - Why Python script works differently when using ansible? 为什么 `is` 运算符在脚本和 REPL 中的行为不同? - Why does the `is` operator behave differently in a script vs the REPL? Python package 导入在 Spyder 中有效,但在 VS Code 中无效 - Python package import works in Spyder but not VS Code 为什么&#39;tensorflow&#39;模块导入在Spyder中失败而在Jupyter Notebook中失败而在Python提示符下失败? - Why does the 'tensorflow' module import fail in Spyder and not in Jupyter Notebook and not in Python prompt? 为什么这在IDE中有效但在CMD提示中不起作用? - Why does this work in IDE but not in CMD Prompt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM