Skip to main content
 首页 » 编程设计

python之遇到提示时遇到问题

2025年05月04日14Leo_wl

我似乎没有得到预期的工作。我似乎听不懂提示。

这是我的:

with settings(hide('commands', 'warnings') , warn_only=True): 
   prompts = expect('Are you sure you want to perform this operation? [Y/N]:', 'N') 
   with expecting(prompts): 
      run(sudo("/something.sh apply /some.file" , user="someuser")) 

我很确定我做错了什么。

请您参考如下方法:

我已经用一些小的东西编辑了你的代码:

  1. 期待需要一个列表
  2. Fabric/Fexpect sudo() 和 run() 不能嵌套。你可能只需要 sudo()
  3. 您必须使用斜杠 \[ 转义正则表达式符号,例如 [] 或只是缩短“expectation”:

    prompts = [] 
    prompts += expect('Are you sure.*', 'N') 
    with expecting(prompts): 
        sudo("/something.sh apply /some.file" , user="someuser") 
    

另外,也许你不应该hide('commands'),这取决于 something.sh 使用的提示。