简体   繁体   中英

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?

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM