Search code examples
pythonpython-3.xpython-2.7passwordswifi

Showing the elif statement when the if statement is true showing the statements more than 1 time


I am having problem in my project just see my code

import subprocess
a=input("WANT SPECIFIC PASSWORD OR OF ALL if all type all if specific type wifi name")
if(a=="all"):
    data=subprocess.check_output(['netsh','wlan','show','profiles']).decode('utf-8').split('\n')
    wifis=[line.split(':')[1][1:-1] for  line in data if"All User Profile"in line]
    for wifi in wifis:
        resilt=subprocess.check_output(['netsh','wlan','show','profile',wifi,'key=clear']).decode('utf-8').split('\n')
        resilt=[line.split(':')[1][1:-1]for line in resilt if "Key Content" in line]
        try:
                print('Name',':',wifi,'Password:'+resilt[0])
        except IndexError:
                print('Name',':',wifi,'Password : Cannot be read!')
else:
    data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')
    wifis = [line.split(':')[1][1:-1] for line in data if "All User Profile" in line]
    for wifi in wifis:
        resilt = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', wifi, 'key=clear']).decode('utf-8').split(
            '\n')
        resilt = [line.split(':')[1][1:-1] for line in resilt if "Key Content" in line]
        if (a in wifi):
            # print('Name :', fg, "\n"'Password:' + resilt[0])
            try:
                print('Name :',a,"\n"'Password:' + resilt[0])
            except IndexError:
                print('Name', ':', a, 'Password zz: There is no password!')

        elif (a not in wifi):
            print("There is no wifi name",a,"in your system")

Here my problem is that when i give input all to get all wifi's password which is in my pc then it is working fine but when I am giving specific wifi name to get it's password then it is giving but.Just see the photo enter image description here

Here it is showing the password but it is also showing "There is no wifi name Priyanshu in your system"here I am giving the correct name.I just only want that it will only show the name and password not any other things and the statement "There is no wifi........"it is showing it 6 times not one time.I just only want the name and password And when I am giving the wrong password then it is showing the statement 6 times not just only one Time.I want only one time. enter image description here

I am just confused what I have to do.


Solution

  • The code is correct but this is the customised version you are asking for

    for wifi in wifis:
            resilt = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', wifi, 'key=clear']).decode('utf-8').split(
                '\n')
            resilt = [line.split(':')[1][1:-1] for line in resilt if "Key Content" in line]
            if (a in wifi):
                # print('Name :', fg, "\n"'Password:' + resilt[0])
                try:
                    print('Name :',a,"\n"'Password:' + resilt[0])
                except IndexError:
                    print('Name', ':', a, 'Password zz: There is no password!')
                break
    else :
            print("There is no wifi name",a,"in your system")
    

    Explanation: If the wifi with desired name is found the code within if block executes. It also executes the break command which terminates the loop. The last else statement only executes if the for loop terminates after it's complete execution. So, if the desired wifi is not found it will execute. But, if the desired wifi is found then the break command will skip the else part.