【问题标题】:When using wifi_profile to get ssid it says this IndentationError: expected an indented block on some line [closed]当使用 wifi_profile 获取 ssid 它说这个 IndentationError: expected an indented block on some line [关闭]
【发布时间】:2021-09-15 19:13:31
【问题描述】:

wifi_profile["ssid"] = name 行不允许代码运行。它说

IndentationError: 需要一个缩进块。

把线改正,这样它就可以通过了。

import subprocess

import re

command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode()

profile_names = (re.findall("All User Profile       : (.*)\r", command_output))

wifi_list = list()

if len(profile_names) != 0:
    for name in profile_names:
        wifi_profile = dict()

        profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode()

        if re.search("Security key           : Absent", profile_info):
            continue
        else:
        
        wifi_profile["ssid"] = name
        
        profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode()
        

        password = re.search("Key Content            : (.*)\r", profile_info_pass)

        if password == None:
            wifi_profile["password"] = None
        else
            wifi_profile["password"] = password[1]
        wifi_list.append(wifi_profile)

for x in range(len(wifi_list)):
    print(wifi_list[x])

【问题讨论】:

  • 去掉上面的else:,如果下面没有代码的话。还有一个错字:在最后一个 else (if password == None:) 之后添加一个 :
  • AttributeError: 'module' 对象没有属性 'run'
  • stackoverflow.com/questions/40590192/…——你用python2吗?
  • 是的,我使用的是 2.7,但当我更改参数时,错误仍然相同。 .call(与

标签: python python-2.7


【解决方案1】:
import subprocess
import re

command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode()

profile_names = (re.findall("All User Profile       : (.*)\r", command_output))

wifi_list = list()

if len(profile_names) != 0:
    for name in profile_names:
        wifi_profile = dict()

        profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode()
        if re.search("Security key           : Absent", profile_info):
            continue
        else:
            wifi_profile["ssid"] = name
        
        profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode()
        password = re.search("Key Content            : (.*)\r", profile_info_pass)

        if password == None:
            wifi_profile["password"] = None
        else: # <----- missing colon
            wifi_profile["password"] = password[1] # <----- this line was not properly indented

        wifi_list.append(wifi_profile)

for x in range(len(wifi_list)):
    print(wifi_list[x])

【讨论】:

  • command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode() AttributeError: 'module' object has no属性“运行”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多