首页 > 解决方案 > 在 if 语句中使用 path.exist(语法错误)

问题描述

我正在尝试测试用户提供的有效性路径作为更复杂脚本的一部分。

我创建了脚本的简化版本,发现我没有正确使用 path.exists 命令。我想使用Trueor的输出False来指导我的if陈述,但它不起作用。

我尝试了以下多种变体:

# get the module I need
import os.path
from os import path

# use a random path just to test for functionality
test_path = "/my/test/path"

#Create a variable based on whether the path actually exists
#so that `True` or `False` can be used in subsequent `if` statements 
#and other decision making processes   
authenticity_test = path.exists(test_path)

#a basic check to see if the variable is actually being set correctly
#(it seems to be)
print(authenticity_test)

#Simplified if statement using only `print()` commands that peristently fails
if authenticity_test = True
    print("Is True")
else:
    print("Is False")

我也尝试过使用字符串,例如替换

authenticity_test = path.exists(test_path)

authenticity_test = str(path.exists(test_path))

然后使用

 if authenticity_test = "True"

错误似乎总是发生在这一行:

if authenticity_test = True

Error: File "<ipython-input-74-c28cf8149b8b>", line 1
if authenticity_test = True
                     ^
SyntaxError: invalid syntax

标签: pythonif-statementpathsyntax-error

解决方案


更正

if authenticity_test:

或者

if authenticity_test==True:

推荐阅读