lundi 3 août 2020

Using path.exist in if statement (syntax error)

I am trying to test the validity paths provided by a user as part of a more complex script.

I have created simplified versions of my script to find that I am not using the path.exists command correctly. I want to use the output of True or False to guide my if statement but it is not working.

I have tried multiple variations of the following:

# 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")

I have also tried using strings e.g. replacing

authenticity_test = path.exists(test_path)

with

authenticity_test = str(path.exists(test_path))

and then using

 if authenticity_test = "True"

The error always seems to occur on this line:

if authenticity_test = True

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

Aucun commentaire:

Enregistrer un commentaire