pythonos.path.exists返回fals

2024-06-03 01:08:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正式迷路了。。我有一个小的python代码来检查文件是否存在。路径存储在字典中。

当我执行下一行代码时,它返回false:

if not os.path.exists(self.parameters['icm_profile']):
    raise FileDoesNotExistError(PreprocessingErrors.FileNotPresent, "icm profile {} not found".format(self.parameters['icm_profile']))

当我复制精确的字符串并执行下一行时,它返回true:

^{pr2}$

因此,路径确实存在。

我想不出有什么不同。。。我到底做错了什么?

enter image description here


Tags: 文件path代码self路径falseif字典
1条回答
网友
1楼 · 发布于 2024-06-03 01:08:37

在我看来你有一对额外的引语,这可能会给你带来麻烦。尝试:

self.parameters['icm_profile'][1:-1]

DeepSpace提到的另一种方法是使用

^{pr2}$

或者你真的偏执狂

self.parameters['icm_profile'].strip('"').strip("'")

相关问题 更多 >