gpt4 book ai didi

python - 检查路径在 Python 中是否有效

转载 作者:太空狗 更新时间:2023-10-29 21:11:28 27 4
gpt4 key购买 nike

有没有简单的方法来检查路径是否有效?该文件现在不一定存在,我想知道它是否可能存在。

我目前的版本是这样的:

try:
f = open(path)
except:
<path invalid>

我正在考虑简单地检查路径是否包含任何 these字符。

最佳答案

您还可以尝试以下方法:

import os  
if not os.path.exists(file_path):
print "Path of the file is Invalid"

关于python - 检查路径在 Python 中是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4102896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com