python - 在 if 语句中使用 path.exist(语法错误)
问题描述
我正在尝试测试用户提供的有效性路径作为更复杂脚本的一部分。
我创建了脚本的简化版本,发现我没有正确使用 path.exists 命令。我想使用True
or的输出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
解决方案
更正
if authenticity_test:
或者
if authenticity_test==True:
推荐阅读
- python-3.x - _tkinter.TclError:使用 wordcloud 时无法连接以显示“localhost:10.0”
- visual-c++ - 在 Visual C++ 中,如何将一个类移动到它自己的文件中?
- c++ - 传递 std::ref(value) 时是否应应用 std::optional 的扣除指南?
- javascript - 注册前接受模式
- python - Django:将上传文件中的文本写入文本字段
- java - EditText 自动转到下一行,输入类似 whatsapp editText 这样的字词
- python - 从 JSON 文件制作数据集
- jsonschema - 如何在 JSON Schema 和 Open API (OAS) 中定义 UUID 属性
- c# - For循环使用数组c#
- java - 使用 Google Cloud 为 Android 应用设置服务器