Pause in Python

后端 未结 13 1181
臣服心动
臣服心动 2020-12-24 10:59

I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.

Afte

相关标签:
13条回答
  • 2020-12-24 11:08

    There's no need to wait for input before closing, just change your command like so:

    cmd /K python <script>
    

    The /K switch will execute the command that follows, but leave the command interpreter window open, in contrast to /C, which executes and then closes.

    0 讨论(0)
  • 2020-12-24 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-12-24 11:09
    import pdb
    pdb.debug()
    

    This is used to debug the script. Should be useful to break also.

    0 讨论(0)
  • 2020-12-24 11:11

    The best option: os.system('pause') <-- this will actually display a message saying 'press any key to continue' whereas adding just raw_input('') will print no message, just the cursor will be available.

    not related to answer:

    os.system("some cmd command") is a really great command as the command can execute any batch file/cmd commands.

    0 讨论(0)
  • 2020-12-24 11:11

    In Windows, you can use the msvcrt module.

    msvcrt.kbhit() Return true if a keypress is waiting to be read.

    msvcrt.getch() Read a keypress and return the resulting character. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed.

    If you want it to also work on Unix-like systems you can try this solution using the termios and fcntl modules.

    0 讨论(0)
  • 2020-12-24 11:18

    One way is to leave a raw_input() at the end so the script waits for you to press enter before it terminates.

    The advantage of using raw_input() instead of msvcrt.* stuff is that the former is a part of standard Python (i.e. absolutely cross-platform). This also means that the script window will be alive after double-clicking on the script file icon, without the need to do

    cmd /K python <script>
    
    0 讨论(0)
提交回复
热议问题