Python from rookie to Master (2): Empty python console

Source: Internet
Author: User


The Execute Python command goes into the Python console. Python statements can be executed interactively in the Python console. That is, executes a line of Python statements and returns the execution result immediately.



When the Python console enters too many Python statements, it is sometimes necessary to empty the statements and execution results that have already been entered and start typing the python statement again. For example, it is a python console that enters multiple Python statements and outputs the corresponding results.






Of course, if you do not want to see these Python statements and output results, you can always press the "enter" key until everything in the Python console is moved to the top of the window. In this case, however, the cursor is at the bottom of the Python console, and the command prompt for the Python console is very ugly, as shown in.






If the reader is using a Mac OS x system, you can directly empty the Python console by pressing the CTRL+L key directly in the Python console. In Windows, there is no shortcut key to empty the Python console, so we need to write Python code to implement the ability to empty the Python console. Therefore, the purpose of this section is now very clear, this section is not to teach you how to empty the Python console, but instead of using the empty Python console, with the knowledge of the import module and declaring variables described in the previous two sections, to write a Python program to empty the python for Windows Console.



Under Windows, if you want to empty the Windows console, execute the CLS command. Because the Python console is implemented using the Windows console, the CLS command can also empty the Python console. However, the Python console cannot directly execute the CLS command. In order to execute external commands in the Python console, you need to invoke the system function in the OS module. Readers can enter the following code in a single line on the Python console.


Import os # import os module
Os.system('cls') # Execute the cls command to empty the Python console


The execution of these two lines of code is to empty the content that was previously entered in the Python console, but the 1th line of the Python console outputs a "0", as shown in.






In fact, this "0" is the return value of the Os.system function. The Python console outputs the return value of each execution statement. The Os.system function returns "0" if the command is executed successfully, and returns "1" if the execution command fails. But to get the most out of it, remove the "0" now.



This "0" is output because the standard output of the Python language is pointing to the Python console by default, so all Python statement execution results are output to the Python console. Now just change the standard output of the Python language to point to a file, and the result of the Os.system function will be written directly to the file, not the Python console.



The following example writes a Python program to empty the Python console.
The steps to empty the Python console without outputting "0" are as follows:



(1) Import the OS module and SYS module.



(2) Open a file in a writable manner using the Open function, this example is Out.log.



(3) In order not to affect the output of other statements in the Python console, you should first save the default Python standard output to a variable to restore the default Python standard output later. Use Sys.stdout to get a handle to the Python standard output (Handler).



(4) The Python standard output points to the 2nd step open file.



(5) Use the Os.system function to execute the CLS command.



(6) Restore Python's default standard output.



The complete implementation code is as follows. The reader can enter the code in a line of the Python console, and when executed to the Os.system (' CLS ') statement, the Python console is emptied and no more "0" is displayed.


Import os # import os module
Import sys # import sys module
F_handler=open('out.log', 'w') # Open the out.log file
Oldstdout = sys.stdout # save the default Python standard output
Sys.stdout=f_handler # Point Python standard output to out.log
Os.system('cls') # Empty the Python console
Sys.stdout = oldstdout # Restore Python default standard output 


"Python from rookie to Master" has been published, buy send video lessons



"Python from rookie to master" knowledge structure map.






Scan QR code focus on the "geek Origins" public number, technical articles, video courses






Welcome to the "Python developer Base" public number





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.