2

I am new to python and I don't have much experience in programming. I am trying to create a script in python using the subprocess and os modules which, when executed, will open the command prompt and ask the user for the name of the WiFi and then the script will create a text file in which it will save the profile output. It is working perfectly by saving the profile output in a text file but the problem is, when the user enters invalid data, it saves the error itself in the text file which I want to display in the command prompt.

import subprocess
import os


def get_wifi_name():
    os.system('color A')
    os.system('cls')
    profile = input("\n[-]Enter name of previously connected WiFi: ")
    output = subprocess.getoutput('netsh wlan show profile ' + profile + ' key=clear')
    return profile, output


profile_name, output_content = get_wifi_name()

if output_content == "Profile " + profile_name + " is not found on the system.":
    os.system("\nPlease enter valid WiFi Network")
else:
    f = open('WiFi.txt', 'w')
    f.write(output_content)
    f.close()
3
  • 1
    os.system is obselete and you should move everything to subprocess. subprocess.communicate will return the usual result and error message separately and you will have better control of them Commented Jan 16, 2021 at 22:10
  • os.system("\nPlease enter valid WiFi Network") should be either os.system("echo Please enter valid WiFi Network") or print("\nPlease enter valid WiFi Network") Commented Apr 28, 2021 at 17:10
  • You should just check if is not found on the system appears in output_content. if "is not found on the system" in output_content: … Commented Apr 28, 2021 at 17:11

1 Answer 1

1

You forgot to add double quotes here:

if output_content == 'Profile "' + profile_name + '" is not found on the system.':

This should work:

import subprocess
import os

def get_wifi_name():
    os.system('color A')
    os.system('cls')
    profile = input("\n[-]Enter name of previously connected WiFi: ")
    output = subprocess.getoutput('netsh wlan show profile ' + profile + ' key=clear')
    return profile, output


profile_name, output_content = get_wifi_name()

if output_content == 'Profile "' + profile_name + '" is not found on the system.':
    print("\nPlease a enter valid WiFi Network")
    input("\npress <enter> to exit")
else:
    f = open('WiFi.txt', 'w')
    f.write(output_content)
    f.close()
    print("\nFile saved successfully")
    input("\npress <enter> to exit")
Sign up to request clarification or add additional context in comments.

1 Comment

we used print(), because os.system() will evaluate the first word from the given string as a command, and the string begins with "please", which is not a command in cmd, also, we used input() after printing something, because the cmd will auto-shutdown if no other commands are being executed (in case of running python files), if my answer worked for you, please consider accepting it :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.