Mike
Mike

Reputation: 554

Why does python script works differently in Spyder vs cmd prompt

I have the following script 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:

path exists

when I run it from the command prompt (python test.py) I get this:

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.

Upvotes: 1

Views: 1450

Answers (1)

Nick is tired
Nick is tired

Reputation: 7056

os.path.exists() started accepting path objects in Python 3.6 and your problem is occuring in your cmd prompts as it is running Python 3.5, change it to 3.6 to fix your problem.

Upvotes: 2

Related Questions