Tolga Yilmaz
Tolga Yilmaz

Reputation: 113

How to prevent command window from closing when a program finishes executing

I made a small program in Python (.py) and converted it into a Windows executable file (.exe) using Py2exe. It asks for a string and then outputs a string -- very simple! -- and works flawlessly in Python.

However, when the exe file finishes execution in the command window, the command window closes automatically before I can get a glimpse of its output (I assume it does print the output because, like I said, it works flawlessly in Python).

How can I prevent this from happening? I assume I need to change my code, but what exactly do I need to add to it?

Here is my code, in case it helps you to see it (it's a word-wrapper):

import string

def insertNewlines(text, lineLength):
    if text == '':
        return ''
    elif len(text) <= lineLength:
        return text
    elif text[lineLength] == ' ':
        return text[:lineLength] + '\n' + insertNewlines(text[lineLength+1:], lineLength)
    elif text[lineLength-1] == ' ':
        return text[:lineLength] + '\n' + insertNewlines(text[lineLength:], lineLength)
    else:
        if string.find(text, ' ', lineLength) == -1:
            return text
        else:
            return text[:string.find(text,' ',lineLength)+1] + '\n' + insertNewlines(text[string.find(text,' ',lineLength)+1:], lineLength)
    print

if __name__ == '__main__':
    text = str(raw_input("Enter text to word-wrap: "))
    lineLength = int(raw_input("Enter number of characters per line: "))
    print 
    print insertNewlines(text, lineLength)

Thank you.

Upvotes: 1

Views: 2235

Answers (3)

paxdiablo
paxdiablo

Reputation: 881093

Just put this at the end of your code:

junk = raw_input ("Hit ENTER to exit: ")

In other words, your main segment should be:

if __name__ == '__main__':
    text = str(raw_input("Enter text to word-wrap: "))
    lineLength = int(raw_input("Enter number of characters per line: "))
    print 
    print insertNewlines(text, lineLength)
    junk = raw_input ("Press ENTER to continue: ")

Upvotes: 1

martineau
martineau

Reputation: 123393

This is what I use in my scripts:

#### windows only ####
import msvcrt

def readch(echo=True):
    "Get a single character on Windows."
    while msvcrt.kbhit():
        msvcrt.getch()
    ch = msvcrt.getch()
    while ch in b'\x00\xe0':
        msvcrt.getch()
        ch = msvcrt.getch()
    if echo:
        msvcrt.putch(ch)
    return ch.decode()

def pause(prompt='Press any key to continue . . .'):
    if prompt:
        print prompt,
    readch()
######################

Sometimes though, I just use the following to make the window stay open for a short time before closing.

import time
time.sleep(3)

Upvotes: 0

grc
grc

Reputation: 23545

The simplest way is probably to use raw_input() just before your program finishes. It will wait until you hit enter before closing.

if __name__ == '__main__':
    text = str(raw_input("Enter text to word-wrap: "))
    lineLength = int(raw_input("Enter number of characters per line: "))
    print 
    print insertNewlines(text, lineLength)
    raw_input()

Upvotes: 1

Related Questions