简体   繁体   English

适用于Python 3.7.1的文件

[英]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). 它始终有效(我使用Python 3.4)。 Now, for some reason, I wanted to change the interpreter and use Python 3.7.1 现在,由于某种原因,我想更改解释器并使用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; Path.resolve()的行为在Python 3.6中已更改; you now need to pass strict=True to make it fail when the file doesn't exist. 您现在需要传递strict=True以使该文件不存在时失败。

But it's probably easier to use Path.exists() instead. 但是使用Path.exists()可能更容易。 (Or os.path.exists() if you want to work with path strings rather than Path objects.) (如果要使用路径字符串而不是Path对象,则为os.path.exists() 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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