How to keep a Python script output window open?

前端 未结 23 2475
挽巷
挽巷 2020-11-22 10:54

I have just started with Python. When I execute a python script file on Windows, the output window appears but instantaneously goes away. I need it to stay there so I can an

相关标签:
23条回答
  • 2020-11-22 11:08

    If you want to run your script from a desktop shortcut, right click your python file and select Send to|Desktop (create shortcut). Then right click the shortcut and select Properties. On the Shortcut tab select the Target: text box and add cmd /k in front of the path and click OK. The shortcut should now run your script without closing and you don't need the input('Hit enter to close')

    Note, if you have more than one version of python on your machine, add the name of the required python executable between cmd /k and the scipt path like this:

    cmd /k python3 "C:\Users\<yourname>\Documents\your_scipt.py"
    
    0 讨论(0)
  • 2020-11-22 11:08
    `import sys,traceback
    sys.exc_info()[0]
    traceback.format_exc()
    print("Press Enter to exit ...")
    input()`
    

    simply write the above code after your actual code. for eg. am taking input from user and print on console hence my code will be look like this -->

    `import sys,traceback
    nam=input("enter your name:")
    print("your name is:-{}".format(nam)) #here all my actual working is done
    sys.exc_info()[0]
    traceback.format_exc()
    print("Press Enter to exit ...")
    input()`
    
    0 讨论(0)
  • 2020-11-22 11:08

    On windows 10 insert at beggining this:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    

    Strange, but it work for me!(Together with input() at the end, of course)

    0 讨论(0)
  • 2020-11-22 11:09

    cmd /k is the typical way to open any console application (not only Python) with a console window that will remain after the application closes. The easiest way I can think to do that, is to press Win+R, type cmd /k and then drag&drop the script you want to the Run dialog.

    0 讨论(0)
  • 2020-11-22 11:10

    On Python 3

    input('Press Enter to Exit...')
    

    Will do the trick.

    0 讨论(0)
  • 2020-11-22 11:11

    A simple hack to keep the window open:

    counter = 0
    
    While (True):
    
        If (counter == 0):
    
            # Code goes here
    
        counter += 1
    

    The counter is so the code won’t repeat itself.

    0 讨论(0)
提交回复
热议问题