gpt4 book ai didi

python - 变量的目录 raw_input

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:30 24 4
gpt4 key购买 nike

尝试让我的脚本的一部分正常工作。此部分将让用户定义 Web 根目录,以便我们可以在该目录中搜索恶意软件。这是我正在研究的部分:我需要能够获取用户输入并将其作为变量添加到搜索功能中。我确信我的做法是错误的。

import os
for root in raw_input("Where is the web root? "):
if os.path.isdir(root):
print "Using %r" % (root)
elif os.path.isdir(root):
print "Directory not found."

我想让脚本检查目录以确保它存在(以防拼写错误),然后继续搜索某些字符串。谢谢!

最佳答案

我猜你需要一个 while 循环:

import os
root = raw_input("Where is the web root? "
while not os.path.isdir(root):
print "Directory not found, try again"
root = raw_input("Where is the web root? ")

print "Using %r" % (root)
#or do something else with root here

关于python - 变量的目录 raw_input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17156473/

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