Python closes immediately after running script - python

FIXED IT BUT WONT LET ME DELETE
As described by the title, I am trying to run a pretty long script that closes immediately. It closes immediately without showing me the output.
Here is what I've tried:
I've attempted adding input('Press ENTER to exit') and it didn't work. I also tried doing that same command but replacing exit with close and putting exit () under it, as well as exit(0)).
I've also attempted opening it with python myfile.py and nothing. I've tried almost everything I could find.
I'm on Windows, I'm running the script directly from the file, I downloaded it and attempted to run (Sorry if this isn't clear I'm new to Python). The script is long so I don't know if it'd be useful putting it here, also it's kind of private stuff.

quit_control=input("Do you know to quit? press 'Y' and enter for quit.")
while quit_control!="Y":
quit_control=input("Do you know to quit? press 'Y' and enter for quit.")

Related

How do I prevent ^C from showing when displayed when pressing CTRL+C

I am trying to prevent ^C from showing when a user presses CTRL+C while my script is running.
Why do I want to prevent that?
Because things like this will happen and it does not look nice:
$ python3 myscript.py
^CYour pressed CTRL+C
I know there is a similar question here, but it does not work in Python
I found an easy method!
# Returning the cursor to home and dont create a new line
print("\r", end="")
# Now we are able to print on the line where ^C would be displayed
print("Your pressed CTRL+C")
Because things like this will happen...
Just my two cents: the fact that ^C displays in the terminal is a good thing. It's confirmation that SIGINT was sent to the process, as expected. Don't try to remove it; instead, as others have suggested, start a new line if you really want to. Or just exit without printing anything additional at all, like lots of other command line applications.

Shell Closing when starting on Windows and adding new lines

I'm new to code and i have a doubt that i did not find anywhere else. When i try to open a file .py using the Python Shell (double click on Windows) it closes. It's not a code problem because i've created a new file, added this line:
print("test")
and the same thing happended. The most stranger thing is that if i press F5 at the editor, everything works fine. The other thing is that with another file, when i press F5 in the editor, the code works fine but adds a lot of new lines before the code (i did not add any new lines to the code). You can see this code that i'm talking about here and the module here or you can see all the code at GitHub
If you mean you're just double clicking the .py file, you see the shell blink into existence for a moment, then disappear, that's expected. A shell is created to run python to run the script, the script runs to completion, then exits, and the shell (no longer having a running program in it) closes as well.
If you want the output to stick around, either:
Launch a cmd.exe window directly, and invoke your program from there (since the cmd.exe window won't exit, the output will stick around), or
Add a call that prevents the program from exiting until you've had a chance to read the output to the end of your program, e.g. input('Press Enter key to exit...') or the like. As long as that input prompt doesn't get a response, the program is still running, and you can see the output.
The .py file closes immediately because it finishes its execution and exits upon the call of SystemExit. The whole process is too fast for you to be able to see the output on the console. If you want to execute a .py file without using the editor and be able to see the output as well, you can try using cmd and execute a line like this
python yourfile.py
In this way, the window will be left open until you manually close it. However, you have to setup properly to be able to use this python command in cmd, and it is very easy as you can search up online.
For second question, it is because you have printed \n 101 times at line 6 & 7. All function/method calls will be executed at the time you have called them. It does not mean that you can assign a function to a variable, and then only execute the function when you call the variable.
Every function will execute its code and return you something (as what has specified using return statement, can be void too). So if you assign a variable x to a print("\n") statement, it will print \n first and return void(nothing) back to your variable x.

getch() in Python displaying wrong output

I tried running this code in python
from msvcrt import getch
while True:
char = getch()
print char
but this is displaying the character 'ΓΏ' infinitely.
Can anyone help me with this
Thanks in advance
I don't blame you for wanting to use IDLE for debugging but PDB, while not as convenient, can do anything that IDLE can do and it doesn't have this problem (at least in Windows XP-W10). Under PDB both kbhit and getche work correctly. Whether debugging or not, in W10 your script's first use of getch returns junk before the user presses a key but subsequent use is clean. This isn't necessary in earlier versions of Windows but it is always a good idea to precede your real use with if kbhit() : getch() to ensure a clean buffer before asking for user input.
It just works fine when I run the same program via command line.
I believe you are trying in an IDE? If you are using pycharm, the same query has been posted to them and it is unanswered for like a year.
Link : Using getch() in pycharm
Pls try the same code in command prompt and let us know.
Updated answer :
getch() requires a working "console" window, and when you run Idle you don't get one as its set to use pythonw.exe. Thus, it's technically not able to process any keystrokes and returns immediately. You could try to verify that by starting Idle from the command line with the standard python. (But you may have to be in that console window to trigger the getch() to return)
I personally verified your program by launching IDLE from command line. Well it does not return that character indefinitely atleast. It doesnt return anything for that matter.
Just out of curiosity , do you really need to use IDLE for this program? Wouldn't the regular command line suffice? If yes, am sorry I couldnt be of much help for this question.
To launch idle from command prompt : python C:\Python27\Lib\idlelib\idle.py
Source for the answer : IDLE does not return getch() characters

python-for-in statement not terminating itself

I am learning python while using for statement loop is not terminating itself to do so I have to use ctrl+z
as u can see nothing is happening after ......
after using ctrl+z the statement got terminated
i m window user currently working on python 2.7.10
i have already read many post for this problem but find non solution please guide me
The interpreter waits for the rest of the for loop, so just press Enter and the loop will execute.
When working in the python interpreter, a line starting with three dots signals that it is waiting for a block to continue (blocks are created by for, if, while, def, class, and other similar statements).
Even if you put everything on one line (which python only allows if the block is exactly one line) as you have done, the interpreter still waits for you to finish the block.
Anytime that enter a statement and see that, you should first make sure that you have finished what you intended to enter (as you have) and then enter just a blank line (just hit enter) on the next line to signal to the interpreter that the block is done.

Write to File after Program Closes

This might sound like a really dulled down question but I have honestly searched everywhere for it but is there a way where once the user clicks the "exit" or "stop" button to stop there program right after you click that it will write data to a file somewhere? or would that be impossible since you closesd that program? I honestly don't know, Here's my try at it Its nothing really because I don't entirely know how to do it, but I just say this
if (onExit):
f = open('file.txt', mode='w')
f.write (data)
f.close
my onExit is just a Boolean and yeah I'm just not sure how to do it, I know how dumb that code looks btw I just didn't know how to show to you guys that I have tried looking for it other then if I showed you my history tab
Clicking an 'exit' button typically does not actually close a program immediately. Instead, the code that runs when that button is pushed also takes care of saving data.
If we are talking about a console application, which is 'closed' by ctrl-c (i.e. a KeyboardInterrupt), you can use a try-except block:
try:
raw_input()
except KeyboardInterrupt:
# save here
raise
Python does support atexit handlers, but they are most likely not the right solution to your problem.
If you're using PyDev on Eclipse, the terminate button (red square) sends a kill message to the system, which in turn will kill your program without executing further code.
As the previous answer says, you can use the atexit module, but that only works when your program ends normally.
See also: Is it possible for Eclipse to terminate gently instead of using SIGKILL?

Categories

Resources