Hack Forums
[Python] Program keeps closing unexpectedly... - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Program keeps closing unexpectedly... (/showthread.php?tid=3995246)



Program keeps closing unexpectedly... - W R T - Jan 13, 2014

Hey HF,

Well I am trying to make a basic repair tool using Python, it's being a real dick to me so I decided to ask for help...

Here's the current code...

PHP Code

<?php 
print("Hello and thanks for using Mike's Desktop AIO Repair Tool!")
raw_input("Press any key to continue...")
print(
"So what would you like to do?")
print(
"1) Scan HDD for errors & bad sectors.")
print(
"2) Fix registry errors.")
print(
"3) Scan for viruses and malware.")
raw_input("Press any key to continue...")

choice == raw_input("Please select an option...")
if 
choice == "1":
    
raw_input("Press any key to continue...")
    print(
"Now scanning HDD for errors!")
    print(
"Test...") 

What I want it to do is, after it says Now scanning HDD for errors! I want it to open command prompt, BUT I want the Python program to stay OPEN.

Can someone tell me how to make python open Command Prompt BUT keep python running as well?

I can't figure that part out :/

Cheers,
Mike


* - Skyla Blue - Jan 13, 2014

(Jan 13, 2014 05:57 PM)Windows RT Wrote:  Hey HF,

Well I am trying to make a basic repair tool using Python, it's being a real dick to me so I decided to ask for help...

Here's the current code...

PHP Code

<?php 
print("Hello and thanks for using Mike's Desktop AIO Repair Tool!")
raw_input("Press any key to continue...")
print(
"So what would you like to do?")
print(
"1) Scan HDD for errors & bad sectors.")
print(
"2) Fix registry errors.")
print(
"3) Scan for viruses and malware.")
raw_input("Press any key to continue...")

choice == raw_input("Please select an option...")
if 
choice == "1":
    
raw_input("Press any key to continue...")
    print(
"Now scanning HDD for errors!")
    print(
"Test...") 

What I want it to do is, after it says Now scanning HDD for errors! I want it to open command prompt, BUT I want the Python program to stay OPEN.

Can someone tell me how to make python open Command Prompt BUT keep python running as well?

I can't figure that part out :/

Cheers,
Mike

Add raw_input() to the end of your script, and that will keep the console window open


* - W R T - Jan 13, 2014

(Jan 13, 2014 06:04 PM)Skyla Blue Wrote:  Add raw_input() to the end of your script, and that will keep the console window open

Still doesn't work, It just closes again.


* - dberube - Jan 13, 2014

If you want to open a command prompt and keep your script open use subprocess.call . ( http://docs.python.org/3.3/library/subprocess.html#subprocess.call )
Using this, python will wait for the called program to exit before continuing.

Edit:
Example:
Code
import subprocess
subprocess.call("cmd.exe")



* - Skyla Blue - Jan 13, 2014

(Jan 13, 2014 06:48 PM)Windows RT Wrote:  Still doesn't work, It just closes again.

Try running it from command prompt, or terminal if you're on linux, and see if it gives you an error


* - dberube - Jan 13, 2014

It is actually very straightforward:
subprocess.call("A command")

"A command" can be anything you would input in a windows terminal.
Code
import subprocess

subprocess.call("notepad.exe")   #Starts notepad
subprocess.call("cmd.exe /c ipconfig")   #Start a command prompt and execute ipconfig



* - W R T - Jan 13, 2014

(Jan 13, 2014 07:32 PM)dberube Wrote:  It is actually very straightforward:
subprocess.call("A command")

"A command" can be anything you would input in a windows terminal.
Code
import subprocess

subprocess.call("notepad.exe")   #Starts notepad
subprocess.call("cmd.exe /c ipconfig")   #Start a command prompt and execute ipconfig

PHP Code

<?php 
print("Hello and thanks for using Mike's Desktop AIO Repair Tool!")
raw_input("Press any key to continue...")
print(
"So what would you like to do?")
print(
"1) Scan HDD for errors & bad sectors.")
print(
"2) Fix registry errors.")
print(
"3) Scan for viruses and malware.")
raw_input("Press any key to continue...")

choice == raw_input("Please select an option...")
if 
choice == "1":
    
import subprocess
subprocess
.call("cmd.exe") 

Current code, I appreciate your help! Now, when I get to where it says Press any key to continue... it just shuts the console down :l

Thanks a ton for the help by the way!

Cheers,
Mike


* - dberube - Jan 13, 2014

1: Always put your imports at the beginning of you script. It's clearer.
2: I just noticed an error in you code:
Code
choice == raw_input("Please select an option...")
#Should be
choice = raw_input("Please select an option...")

Always remember:
"x == y" test if x is equal to y
"x = y" assign the value of y to x

3: For your error, try to run this code:
The only reason I see to stop working at raw_input is that you are using python 3.
Code
import subprocess

try:
    print("Just checking if you run python2...")
    raw_input
    print("Looks good to me!")
except:
    print("OOPS, seems you are running python 3.")
    print("Python 3 use input and not raw_input.")
    print("I'll fix this for you now. Next time use 'input'!\n\n")
    raw_input = input

print("Hello and thanks for using Mike's Desktop AIO Repair Tool!")
raw_input("Press any key to continue...")
print("So what would you like to do?")
print("1) Scan HDD for errors & bad sectors.")
print("2) Fix registry errors.")
print("3) Scan for viruses and malware.")
raw_input("Press any key to continue...")

choice = raw_input("Please select an option...")
if choice == "1":
    raw_input("Press any key to continue...")
    print("Now scanning HDD for errors!")
    print("Test...")
    subprocess.call("cmd.exe")



* - W R T - Jan 13, 2014

(Jan 13, 2014 07:53 PM)dberube Wrote:  1: Always put your imports at the beginning of you script. It's clearer.
2: I just noticed an error in you code:
Code
choice == raw_input("Please select an option...")
#Should be
choice = raw_input("Please select an option...")

Always remember:
"x == y" test if x is equal to y
"x = y" assign the value of y to x

3: For your error, try to run this code:
The only reason I see to stop working at raw_input is that you are using python 3.
Code
import subprocess

try:
    print("Just checking if you run python2...")
    raw_input
    print("Looks good to me!")
except:
    print("OOPS, seems you are running python 3.")
    print("Python 3 use input and not raw_input.")
    print("I'll fix this for you now. Next time use 'input'!\n\n")
    raw_input = input

print("Hello and thanks for using Mike's Desktop AIO Repair Tool!")
raw_input("Press any key to continue...")
print("So what would you like to do?")
print("1) Scan HDD for errors & bad sectors.")
print("2) Fix registry errors.")
print("3) Scan for viruses and malware.")
raw_input("Press any key to continue...")

choice = raw_input("Please select an option...")
if choice == "1":
    raw_input("Press any key to continue...")
    print("Now scanning HDD for errors!")
    print("Test...")
    subprocess.call("cmd.exe")

I really do appreciate the time, honestly, you helped me out a TON. Last question, would there be anyway that I can make Python run cmd as administrator? Or would I need to convert python to an .exe and make someone run it as admin?

Cheers,
Mike


* - dberube - Jan 13, 2014

(Jan 13, 2014 08:00 PM)Windows RT Wrote:  I really do appreciate the time, honestly, you helped me out a TON. Last question, would there be anyway that I can make Python run cmd as administrator? Or would I need to convert python to an .exe and make someone run it as admin?

Cheers,
Mike

Converting the script to an .exe is an option but I think running the interpreter as admin would also work.

That is, maybe google know a way to elevate privilege from the command prompt?


* - Tirus - Jan 13, 2014

Put this at the end of your code

Code
import os
os.system("pause")

or you can do

Code
import time
sleep(100) # this sleeps for 100 seconds