Rebagliati Loris
Rebagliati Loris

Reputation: 579

File Exist For Python 3.7.1

in my project I'm doing, I've always used this part of the script to check if a file existed or not:

path = os.path.join("myfile.txt")
conf = Path(path)
try:
    lc = conf.resolve()
except FileNotFoundError:
    print("new")
else:
    print("load")

It always worked (I used Python 3.4). Now, for some reason, I wanted to change the interpreter and use Python 3.7.1

Now, I only get printed "load", regardless of whether the file exists or not. How should I solve?

Upvotes: 1

Views: 1080

Answers (1)

David Z
David Z

Reputation: 131660

The behavior of Path.resolve() changed in Python 3.6; you now need to pass strict=True to make it fail when the file doesn't exist.

But it's probably easier to use Path.exists() instead. (Or os.path.exists() if you want to work with path strings rather than Path objects.)

Upvotes: 5

Related Questions